Python使用requests_html模块时遇Chromium下载OSError问题求助
解决requests_html调用render时Chromium下载失败的问题
问题描述
使用Python的requests_html模块进行网页动态渲染时,调用render()方法出现Chromium下载失败的问题,代码片段如下:
response = session.get(url) response.html.render(timeout=20)
触发的错误信息:
OSError: Chromium downloadable not found at https://storage.googleapis.com/chromium-browser-snapshots/Win_x64/1181205/chrome-win.zip: Received
The specified key does not exist.No such object: chromium-browser-snapshots/Win_x64/1181205/chrome-win.zipreturn (<Code>NoSuchKey</Code>)
已尝试更换requests_html版本、寻找自定义可执行文件路径的方法,但均未解决。
解决方案
方案1:直接指定本地Chrome/Chromium可执行文件路径
如果本地已安装Chrome或Chromium,直接在render()方法中通过executable_path参数指定其路径即可:
- Windows示例:
response = session.get(url) response.html.render(timeout=20, executable_path="C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")
- macOS示例:
response = session.get(url) response.html.render(timeout=20, executable_path="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome")
- Linux示例:
response = session.get(url) response.html.render(timeout=20, executable_path="/usr/bin/google-chrome")
方案2:通过环境变量指定Chromium路径
- 手动下载对应系统的可用Chromium快照版本,解压后找到可执行文件的完整路径;
- 设置系统环境变量
CHROMIUM_EXECUTABLE_PATH,值为上述可执行文件路径; - 重启Python运行环境,原代码无需修改,requests_html会自动读取该环境变量并使用指定的浏览器。
方案3:升级requests_html到最新版本
部分旧版本的requests_html绑定的Chromium版本已被官方移除,升级到最新版本会更新默认的Chromium版本号,解决下载路径无效的问题:
pip install --upgrade requests-html
内容的提问来源于stack exchange,提问作者Satya Bharadwaj
相关产品推荐
相关产品推荐

