You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python Selenium无法加载Bet365.com的问题求助

解决Selenium访问Bet365时页面持续加载的问题

以下是针对你遇到的页面加载器一直转动问题的可行解决方案:

1. 修正用户数据目录的配置错误

你的代码中直接将Profile 1作为user-data-dir的路径是错误的,正确的做法是将Chrome的用户数据根目录设为user-data-dir,再通过profile-directory指定具体的子配置文件:

# 使用原始字符串避免转义问题
profile_root = r"C:\Users\Faiq-81-LP\AppData\Local\Google\Chrome\User Data"
options.add_argument(f"--user-data-dir={profile_root}")
options.add_argument("--profile-directory=Profile 1")

错误的路径配置会导致Chrome无法正确加载用户配置,进而引发页面加载异常。

2. 规范使用undetected-chromedriver

之前的尝试可能因初始化方式不当失效,以下是正确的结合配置的使用示例:

from undetected_chromedriver import Chrome, ChromeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

options = ChromeOptions()
# 保留必要的反检测配置,移除重复项
options.add_argument('--disable-blink-features=AutomationControlled')
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)
# 正确配置用户数据目录
profile_root = r"C:\Users\Faiq-81-LP\AppData\Local\Google\Chrome\User Data"
options.add_argument(f"--user-data-dir={profile_root}")
options.add_argument("--profile-directory=Profile 1")
options.add_argument("window-size=1400,800")

driver = Chrome(options=options)
driver.get("https://www.bet365.com")

# 用显式等待替代固定sleep,等待核心元素加载
try:
    WebDriverWait(driver, 30).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, ".hm-MainHeaderContainer"))
    )
    print("页面加载完成")
except Exception as e:
    print(f"加载超时: {str(e)}")
finally:
    time.sleep(5)
    driver.quit()

undetected-chromedriver会自动修改Selenium的特征值,但需确保配置正确,同时用显式等待替代固定sleep能更精准判断页面加载状态。

3. 添加更多真实浏览器模拟配置

可以补充以下参数增强伪装,降低被Bet365检测的概率:

  • options.add_argument("--start-maximized"):最大化窗口,模拟真实用户操作习惯
  • options.add_argument("--disable-gpu"):禁用GPU加速,避免部分环境下的渲染异常
  • options.add_argument("--disable-extensions"):关闭浏览器扩展,减少自动化特征
  • 随机化User-Agent(需先安装fake_useragent库:pip install fake_useragent):
from fake_useragent import UserAgent
ua = UserAgent()
options.add_argument(f'user-agent={ua.random}')

4. 检查代理配置(若使用)

如果使用代理,需确保:

  • 代理为高匿类型,避免暴露真实IP或代理特征
  • 代理地区与Bet365账号所在地区匹配,否则会触发地域限制
  • 代理配置格式正确:
options.add_argument('--proxy-server=http://你的代理IP:端口')

若代理需要认证,需通过浏览器扩展或其他方式处理(Selenium直接配置带认证的代理较为复杂)。

5. 清理冲突配置

移除代码中重复或不必要的参数,比如重复的--disable-popup-blocking、非必需的--remote-debugging-port=9222,这些冗余配置可能成为Bet365的检测特征。

内容的提问来源于stack exchange,提问作者Faiq Yazdani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 01:04:48