谷歌地图一些 XPATH 选择器返回一些不是 Selenium Python 的数据

我试图抓取谷歌地图。phone 和 hours 变量不返回任何数据。其他变量工作正常并返回数据。XPATH 是正确的。我不确定这里有什么问题。

其他选择器(如姓名、地址、职务、网站)可以正常返回数据,但电话和营业时间不会返回任何数据。

希望得到一些答案。

from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC

from selenium.common.exceptions import TimeoutException

from selenium.webdriver.support.ui import Select

from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.common.keys import Keys

from scrapy.selector import Selector

import csv

from tqdm import tqdm

import time


driver = 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"]'))

            )


湖上湖
浏览 123回答 1
1回答

月关宝盒

您可以使用 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()

动漫人物

尝试谷歌地图链接而不是谷歌搜索:https://www.google.com/maps/place/Leduc+Plumbing+and+Heating/@53.274672,-113.5486679,17z/data=!3m1!4b1!4m5!3m4! 1s0x539ff9a5d31a87c9:0xf494d91aafd55e55!8m2!3d53.2746688!4d-113.5464739IT应该更稳定。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python