It tried selenium to scrap the indiamart website but i didn’t get errors. now i am not getting the output. i don’t know where i went wrong.
here i am copying the code along with output. I want output in dataframes the data to be extract
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
import os
# Set up the service for ChromeDriver
service = Service(r'C:UsersvamsiOneDriveDocumentsSeleniumchromedriver.exe')
# Initialize the Chrome WebDriver with the service
driver = webdriver.Chrome(service=service)
#URL visit
driver.get("https://dir.indiamart.com/impcat/boneless-chicken.html")
driver.maximize_window()
#Locate elements using the updated methods
company_names = driver.find_elements(By.XPATH, '//h2[@class="lcname"]')
address = driver.find_elements(By.XPATH, '//p[@class="sm clg"]')
view_mobilenumber = driver.find_elements(By.XPATH, '//a[@class="mo viewn vmn"]')
contactlist = driver.find_elements(By.XPATH, '//span[@class="pns-h duet"]')
namelist = []
addresslist = []
numberlist = []
# Calculate the minimum length to avoid index errors
min_length = min(len(names), len(address), len(contactlist))
for i in range(min_length):
name = comapany_names[i].text
addresslist = address[i].text
view_mobilenumber.click()
contact = contactlist[i].get_attribute("innerHTML")
namelist.append(names)
addresslist.append(addresslist)
numberlist.append(contact)
#Intialise data of lists
data = {'company_names': namelist, 'address': addresslist, 'contactlist': numberlist}
df = pd.DataFrame(data)
# Display the DataFrame
print(df)
output: - Empty DataFrame
Columns: [company_names, address, contactlist]
Index: []
driver.quit()
New contributor
Poreddy Harini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.