它尝试使用 Selenium 来废弃 Indiamart 网站,但我没有收到任何错误。现在我没有得到输出。我不知道我哪里错了。
我在这里复制代码和输出。我想要数据帧中的输出来提取数据。
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:\Users\vamsi\OneDrive\Documents\Selenium\chromedriver.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()
主要问题在第 25 行,其中“min_length”计算为零(因为“len(contactlist)”等于零)并且 for 循环甚至没有启动。
我浏览了该网站,我意识到在第 20 行,“contactlist”未正确获取,xpath 中的破折号必须替换为下划线,如下所示:
contactlist = driver.find_elements(By.XPATH, '//span[@class="pns_h duet"]')
这将为您提供以下输出:
company_names address contactlist
0 Mutton Mahal Greater Kailash 1, New Delhi +91-7942557767
1 Khan'S Royal Farm Park Street, Kolkata +91-8043855528
2 +91-8048605624
3 +91-8048986450
4 +91-7942866137
5 +91-8047301237
6 +91-8045166501
7 +91-8046064367
8 +91-8043891734
9 +91-7942803290
10 +91-8046031196
11 +91-8048619287
12 +91-8046062625
13 +91-8048618867
14 +91-8047645601
15 +91-7942669714
16 +91-7942965588
17 +91-8045736337
18 +91-8043892531
19 +91-8043829835
20 +91-7942533404
21 +91-7942798497
22 +91-7942723397
23 +91-7942673670
24 +91-7949092350
25 +91-8048968537
26 +91-7942966884
27 +91-8047686652