chrome被墙
国内网络环境适配 + 改进建议(附已实现方案)
在国内网络环境下使用 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-320 — get_chromedriver_path() 函数
damai/concert.py:16 — from check_environment import get_chromedriver_path
damai/concert.py:35-41 — Concert.__init__ 硬编码 webdriver.Chrome
方案:Edge 优先 + 本地驱动三级查找
Edge 是 Windows 11 自带浏览器,与 Chrome 同 Chromium 内核,完全兼容 Selenium。Edge WebDriver 可从 Microsoft 官网直接下载(国内可正常访问)。
驱动查找优先级:
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 自动转换 + 移动端模拟
# 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.comntp.tencent.comcn.ntp.org.cn
五、pip 镜像安装说明
建议
README 中为国内用户补充 pip 镜像安装说明:
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple以上问题 1-3 均已在本地 fork 中实现并验证通过(Edge + Windows 11 + Python 3.12),如需可提 PR。
Source: WECENG/ticket-purchase