我正在嘗試抓取資料,但他們會給我一個錯誤,這是網站鏈接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)
uj5u.com熱心網友回復:
而不是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)
uj5u.com熱心網友回復:
當你在一個專案上提取 xpath 時,你必須改變一點 xpath,用“.//”說在當前節點上查找
改變
title=item.find_element_by_xpath("//div[@class='name']").text
到
title=item.find_element_by_xpath(".//div[@class='name']").text
結帳并反饋是否有效
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361653.html
上一篇:為什么我的代碼在第一項上回圈
