chrome被墙

Author: Mob1SCreated May 21, 2026Updated Jun 1, 2026

国内网络环境适配 + 改进建议(附已实现方案)

在国内网络环境下使用 WECENG/ticket-purchase 遇到了一系列问题,以下是完整总结和解决方案,均已本地验证通过。


一、ChromeDriver 被墙 核心问题

现象

check_environment.py 中的 get_chromedriver_path() 依赖 chromedriver_autoinstaller,后者从 Google 域名下载 ChromeDriver:

  • googlechromelabs.github.io → 连接超时
  • storage.googleapis.com → 连接超时
  • chromedriver.storage.googleapis.com → 连接超时

同时 pip install 也超时,需加 -i https://pypi.tuna.tsinghua.edu.cn/simple

当前代码位置

damai/check_environment.py:285-320get_chromedriver_path() 函数

damai/concert.py:16from check_environment import get_chromedriver_path

damai/concert.py:35-41Concert.__init__ 硬编码 webdriver.Chrome

方案:Edge 优先 + 本地驱动三级查找

Edge 是 Windows 11 自带浏览器,与 Chrome 同 Chromium 内核,完全兼容 Selenium。Edge WebDriver 可从 Microsoft 官网直接下载(国内可正常访问)。

驱动查找优先级:

python
1. %USERPROFILE%\.edgedriver\msedgedriver.exe  # 一次性手动下载
2. %USERPROFILE%\.chromedriver\chromedriver.exe
3. PATH 中的 msedgedriver / chromedriver
4. webdriver-manager / chromedriver_autoinstaller 自动下载(兜底)

具体改动建议:

  • check_environment.py 增加 _get_edge_paths()_get_msedgedriver_paths() 函数
  • concert.py__init__ 中根据可用驱动自动选择 Edge 或 Chrome
  • README 补充 Edge WebDriver 下载指引:developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

二、大麦"请用手机 App"限制

问题

concert.py 已通过 'm.damai.cn' in self.driver.current_url 检测移动端(L168),但没有做 URL 自动转换。用户复制桌面端链接 detail.damai.cn 后,页面会提示"请用手机 App 购买"。

方案:URL 自动转换 + 移动端模拟

python
# URL 转换
detail.damai.cn/item.htm?id=XXXXX
  -> m.damai.cn/damai/detail/item.html?itemId=XXXXX

# 移动端模拟(在 Chrome/Edge Options 中设置)
mobile_emulation = {
    "deviceMetrics": {"width": 390, "height": 844, "pixelRatio": 3.0},
    "userAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 ...) ..."
}
options.add_experimental_option("mobileEmulation", mobile_emulation)

建议在 Concert.__init__config.py 中增加 URL 自动转换逻辑,同时添加 mobile_emulation 选项。


三、硬编码 CSS 选择器脆弱性

问题

concert.py 多处硬编码了大麦的 CSS class 名称,页面改版即失效:

位置 硬编码选择器
L197 buy__button__text
L198 buy-link
L297-L300 sku-times-card, bui-dm-sku-card-item
L306-L307 sku-tickets-card, item-content
L313 bui-dm-sku-counter
L316 number-edit-bg
L320 bui-btn-contained

虽然 _select_option_by_config() 有一定的容错能力(continue 跳过异常),但如果容器本身找不到,整个选票流程会静默失败。

方案

  • 增加多选择器回退策略(text 匹配 → class 匹配 → XPath 匹配 → 坐标兜底)
  • 选择器失败时输出更有用的调试信息(当前 _scan_page_info()_scan_page_text() 已有基础)

四、NTP 时间同步建议

现状

目前没有时间同步机制,依赖本地系统时间。本地时间可能存在秒级偏差,在抢票场景下影响成功率。

建议

增加 NTP 时间同步(使用 ntplib),计算本地与 NTP 服务器的偏移量,提前点击时考虑该偏移。国内可用 NTP 服务器:

  • ntp.aliyun.com
  • ntp.tencent.com
  • cn.ntp.org.cn

五、pip 镜像安装说明

建议

README 中为国内用户补充 pip 镜像安装说明:

bash
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

以上问题 1-3 均已在本地 fork 中实现并验证通过(Edge + Windows 11 + Python 3.12),如需可提 PR。