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

如何用Python Requests库下载ChromeDriver而非对应网页?

ChromeDriver下载错误:下载网页而非文件的解决方法

你出错的核心原因是构造的下载链接指向的是文件索引网页,而非直接的文件资源地址。你用的https://chromedriver.storage.googleapis.com/index.html?path=...是Google Cloud Storage的文件列表页面URL,请求这个地址返回的是网页HTML内容,自然会得到损坏的"zip文件"。

正确的解决方法

直接构造ChromeDriver压缩包的直接下载链接,格式为:https://chromedriver.storage.googleapis.com/{版本号}/chromedriver_win32.zip,不需要额外的index.html?path=参数。

修改后的完整代码:

import requests

# 获取指定大版本的ChromeDriver最新版本号
chromedriverVersionUrl = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_100"
response = requests.get(chromedriverVersionUrl)
latestVersionNumber = response.text.strip()  # 移除版本号可能带的换行/空白字符

# 构造正确的直接下载链接
downloadUrl = f"https://chromedriver.storage.googleapis.com/{latestVersionNumber}/chromedriver_win32.zip"

# 发起请求并保存文件(用with上下文管理器更安全)
r = requests.get(downloadUrl, allow_redirects=True)
if r.status_code == 200:
    with open('chromedriver.zip', 'wb') as f:
        f.write(r.content)
else:
    print(f"下载失败,状态码:{r.status_code}")

额外提示

  • 调用strip()处理版本号:避免接口返回的文本包含换行符或空白,导致链接拼接错误
  • 使用with open:确保文件操作完成后自动关闭文件句柄,避免资源泄漏
  • 增加状态码检查:可以快速判断请求是否成功,方便排查问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 04:37:06