繁体   English   中英

如何使用 selenium 从网站抓取数据

[英]How to scrape data from website using selenium

我试图抓取数据,但他们会给我一个错误,这是网站链接https://www.dastelefonbuch.de/Suche/Zahnarzt

from selenium import webdriver
PATH="C:\Program Files (x86)\chromedriver.exe"
url='https://www.dastelefonbuch.de/Suche/Zahnarzt'
driver =webdriver.Chrome(PATH)
driver.get(url)
vid=driver.find_element_by_xpath("//div[@class='vcard']")
for item in vid:
    title=item.find_element_by_xpath("//div[@class='name']").text
    phone=item.find_element_by_xpath("//span[@class='nr']").text
    website=item.find_element_by_xpath("//i[@class='icon icon_url']").text
    print(title,phone,website)
    

而不是vid=driver.find_element_by_xpath("//div[@class='vcard']")返回一个WebElement你需要vid=driver.find_element_by_xpath("//div[@class='vcard']")这将返回一个列表进行迭代,如下所示:

from selenium.webdriver.common.by import By

vid = driver.find_elements(By.XPATH, "//div[@class='vcard']")
for item in vid:
    title=item.find_element_by_xpath("//div[@class='name']").text
    phone=item.find_element_by_xpath("//span[@class='nr']").text
    website=item.find_element_by_xpath("//i[@class='icon icon_url']").text
    print(title,phone,website)

当您在一个项目上提取 xpath 时,您必须更改一点 xpath,用“.//”表示在当前节点上查找

改变

title=item.find_element_by_xpath("//div[@class='name']").text

title=item.find_element_by_xpath(".//div[@class='name']").text

结帐并反馈是否有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM