使用undetected-chromedriver加载页面失败问题求助
解决undetected-chromedriver加载bet365页面卡住的问题
bet365反爬机制严苛,基础配置的undetected-chromedriver极易被识别,以下是针对性调整方案:
一、强化ChromeOptions反检测配置
添加更贴近真实用户的浏览器参数,规避自动化标识:
- 禁用自动化控制特征
- 设置随机真实用户代理
- 关闭扩展、弹窗拦截、开发者工具提示等干扰项
- 启用沙箱模式,避免环境异常
二、优化驱动初始化逻辑
指定Chrome版本避免兼容性问题,同时移除驱动自带的自动化标识。
三、调整页面加载逻辑
用显式等待判断页面加载状态,替代固定sleep,同时避免无意义的重复请求。
修改后的代码示例
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import undetected_chromedriver as uc from random_user_agent.user_agent import UserAgent from random_user_agent.params import SoftwareName, OperatingSystem # 生成随机真实用户代理 software_names = [SoftwareName.CHROME.value] operating_systems = [OperatingSystem.WINDOWS.value, OperatingSystem.LINUX.value] user_agent_rotator = UserAgent(software_names=software_names, operating_systems=operating_systems, limit=100) random_ua = user_agent_rotator.get_random_user_agent() # 配置ChromeOptions options = uc.ChromeOptions() options.headless = False # 核心反检测参数 options.add_argument('--disable-blink-features=AutomationControlled') options.add_argument(f'user-agent={random_ua}') options.add_argument('--disable-extensions') options.add_argument('--disable-popup-blocking') options.add_argument('--start-maximized') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--disable-infobars') options.add_argument('--disable-notifications') # 禁用自动化相关扩展和开关 options.add_experimental_option('excludeSwitches', ['enable-automation']) options.add_experimental_option('useAutomationExtension', False) # 初始化驱动,可指定Chrome版本(如本地版本固定,添加version_main=114) driver = uc.Chrome(use_subprocess=True, options=options) # 移除navigator.webdriver标识 driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") try: url = "https://www.bet365.gr/#/IP/B1" driver.get(url) # 显式等待页面关键元素加载,超时30秒,可替换为页面实际存在的元素选择器 WebDriverWait(driver, 30).until( EC.presence_of_element_located((By.TAG_NAME, 'body')) ) print("页面加载完成") # 后续业务逻辑编写在这里 # ... finally: driver.quit()
额外注意事项
- 避免频繁刷新页面,高频请求会触发bet365的反爬限制
- 若仍卡住,可尝试更换IP,bet365会对异常IP进行拦截
- 确保本地Chrome版本与undetected-chromedriver适配,版本不匹配也会导致加载异常
内容的提问来源于stack exchange,提问作者user1246950
相关产品推荐
相关产品推荐

