【问题标题】:Selenium - Google Travel Scraping Price History missingSelenium - 谷歌旅行刮价格历史丢失
【发布时间】:2021-08-05 04:30:03
【问题描述】:

我正在使用此 python 脚本返回 html,但它不返回价格历史记录(见屏幕截图)。使用非硒浏览器确实会返回带有价格的 html(即使没有通过简单的正则表达式扩展本节); chrome/safari/firefox 都可以,隐身也可以。

from selenium import webdriver
import time

url = 'https://www.google.com/flights?hl=en#flt=SFO.JFK.2021-06-01*JFK.SFO.2021-06-07'

options = webdriver.ChromeOptions()

driver = webdriver.Chrome(options=options)
driver.get(url)
time.sleep(10)
html = driver.page_source
print(html)

driver.quit() 

我无法确定它是否是 chromedriver 中的某些设置。之所以可以这样做,是因为目前有一个 3rd 方抓取工具返回此数据。

试过这个没有用。 Can a website detect when you are using Selenium with chromedriver?

任何想法表示赞赏。

【问题讨论】:

    标签: python selenium google-chrome selenium-chromedriver selenium-firefoxdriver


    【解决方案1】:

    在我添加chrome_options.add_argument("--disable-blink-features=AutomationControlled") 之后,我开始看到这个块。不知道为什么它并不总是加载。

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    
    from selenium.webdriver.chrome.options import Options
    
    url = 'https://www.google.com/flights?hl=en#flt=SFO.JFK.2021-06-01*JFK.SFO.2021-06-07'
    chrome_options = Options()
    chrome_options.add_argument("start-maximized")
    chrome_options.add_argument("--disable-blink-features=AutomationControlled")
    driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver', chrome_options=chrome_options)
    driver.get(url)
    # wait = WebDriverWait(driver, 20)
    # wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".EA71Tc.q7Eewe")))
    time.sleep(10)
    history = driver.find_element_by_css_selector(".EA71Tc.q7Eewe").get_attribute("innerHTML")
    print(history)
    

    这里返回完整的块,包括所有的标签名称。如您所见,我尝试了显式等待,但该块不可见。尝试添加另一个显式等待。

    【讨论】:

    • 谢谢,关于如何在无头模式下返回它的任何想法,目前没有,只有 GUI。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2012-09-19
    相关资源
    最近更新 更多