亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

谷歌地圖一些 XPATH 選擇器返回一些不是 Selenium Python 的數據

谷歌地圖一些 XPATH 選擇器返回一些不是 Selenium Python 的數據

湖上湖 2023-06-20 17:22:39
我試圖抓取谷歌地圖。phone 和 hours 變量不返回任何數據。其他變量工作正常并返回數據。XPATH 是正確的。我不確定這里有什么問題。其他選擇器(如姓名、地址、職務、網站)可以正常返回數據,但電話和營業時間不會返回任何數據。希望得到一些答案。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 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超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()



查看完整回答
反對 回復 2023-06-20
?
反對 回復 2023-06-20
  • 1 回答
  • 0 關注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號