Selenium Python无法渲染英国航空页面动态数据,是否存在反自动化检测机制?
Selenium Python无法渲染英国航空页面动态数据,是否存在反自动化检测机制?
我想用Selenium Python抓取英国航空的航班价格和选项,但遇到了页面动态内容加载失败的问题,具体情况如下:
我的代码
import time from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Set up ChromeDriver service service = Service(r"C:\Users\Rob\Downloads\chromedriver-win64\chromedriver-win64\chromedriver.exe") # Update path to ChromeDriver # Set up Chrome options chrome_options = Options() chrome_options.add_argument("--start-maximized") # Start browser maximized chrome_options.add_argument("--disable-blink-features=AutomationControlled") # Helps bypass bot detection chrome_options.add_argument("--disable-infobars") # Removes Chrome's automation banner # Initialize WebDriver for Chrome driver = webdriver.Chrome(service=service, options=chrome_options) try: # Open the specified URL driver.get("https://www.britishairways.com/travel/book/public/en_gb/flightList?onds=LON-NYC_2025-04-01,NYC-LON_2025-04-10&ad=1&yad=0&ch=0&inf=0&cabin=M&flex=LOWEST&ond=1") # Wait for the cookie consent button and click it try: cookie_button = WebDriverWait(driver, 15).until( EC.element_to_be_clickable((By.ID, "ensAcceptAll")) ) cookie_button.click() print("Cookie consent accepted.") except Exception as e: print("Cookie consent button not found or not clickable:", e) # Optional: Add a delay to observe the page in the browser time.sleep(60) finally: # Close the browser driver.quit()
问题背景
我没有权限使用IAG开发者工具API,所以只能通过网页抓取的方式实现需求。目标URL是我在英国航空主预订页输入以下搜索条件后跳转的页面:伦敦往返纽约,2025年4月1日出发、4月10日返回,经济舱1位成人。
当我直接用Chrome访问这个目标URL时,通过开发者工具的Network标签能找到一个返回航班核心信息(价格、时间)的POST请求,请求地址是:
https://www.britishairways.com/api/sc4/badotcomadapter-paatwo/rs/v1/flightavailability/search;ondwanted=1?metasearchdata=LON_NYC_2025-04-01&metasearchdata=NYC_LON_2025-04-10&locale=en_GB
但用Selenium打开这个目标URL时,虽然代码能成功点击cookie同意弹窗,页面的动态内容却始终加载不出来。而且在Selenium打开的页面的Network标签里,完全找不到这个关键的POST请求。
我还试过另一种方式:先打开英国航空的主预订页,再用代码模拟输入搜索参数跳转,但结果还是一样——目标页面的动态内容始终无法加载。
控制台输出
DevTools listening on ws://127.0.0.1:56232/devtools/browser/b219c155-3fa3-464e-9ed3-ba1330ad5f76
Cookie consent accepted.
Created TensorFlow Lite XNNPACK delegate for CPU.
我怀疑是不是网站的反自动化检测机制识别出了Selenium,从而阻止了动态数据的加载?有没有人能帮我分析原因,或者提供解决思路?
备注:内容来源于stack exchange,提问作者user296950
相关产品推荐
相关产品推荐

