我試圖抓取谷歌地圖。phone 和 hours 變量不返回任何數(shù)據(jù)。其他變量工作正常并返回數(shù)據(jù)。XPATH 是正確的。我不確定這里有什么問題。其他選擇器(如姓名、地址、職務(wù)、網(wǎng)站)可以正常返回數(shù)據(jù),但電話和營業(yè)時間不會返回任何數(shù)據(jù)。希望得到一些答案。from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.common.exceptions import TimeoutExceptionfrom selenium.webdriver.support.ui import Selectfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.keys import Keysfrom scrapy.selector import Selectorimport csvfrom tqdm import tqdmimport timedriver = webdriver.Firefox()linksFile=open("links.txt",'r')allLinks = linksFile.readlines()? ??for link in tqdm(allLinks):? ? try:? ? ? ? driver.get(link)? ? except Exception:? ? ? ? print('Something went wrong with the URL: ')? ??? ? # time.sleep(15)? ??? ? while True:? ? ? ? WebDriverWait(driver, 15).until(? ? ? ? ? ? EC.presence_of_element_located((By.XPATH, '//div[contains(text(), "Directions")] | //div[contains(text(), "Website")]'))? ? ? ? )? ? ? ? results = driver.find_elements_by_xpath('//div[contains(text(), "Directions")] | //div[contains(text(), "Website")]')? ? ? ? for result in results:? ? ? ? ? ? # writing to the CSV file? ? ? ? ? ? outFile =? open("data.csv",'a+',newline="")? ? ? ? ? ? writer = csv.writer(outFile)? ? ? ? ? ? business = driver.find_element_by_xpath('//div[@role="heading"]/div')? ? ? ? ? ? business.click()? ? ? ? ? ? # waiting for the page to load? ? ? ? ? ? WebDriverWait(driver, 15).until(? ? ? ? ? ? ? ? EC.presence_of_element_located((By.XPATH, '//div[@class="immersive-container"]'))? ? ? ? ? ? )
1 回答

月關(guān)寶盒
TA貢獻(xiàn)1772條經(jīng)驗 獲得超5個贊
您可以使用 Java 腳本 outerHTML intead of pageSource 嗎?
response = Selector( driver.execute_script("return document.documentElement.outerHTML"))
在小時的 xpath 中也有一個問題:
hours = response.xpath('//a[contains(text(), "Hours")]/parent::span/following-sibling::div/label/span//b/text()').get()

動漫人物
TA貢獻(xiàn)1815條經(jīng)驗 獲得超10個贊
添加回答
舉報
0/150
提交
取消