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

如何在PythonAnywhere上使用在线WebDriver运行Selenium爬虫代码?

解决PythonAnywhere上Selenium ChromeDriver的配置问题

最优方案:利用PythonAnywhere内置的Chrome环境

PythonAnywhere平台已经预装了Chrome浏览器和对应的ChromeDriver,完全不需要依赖本地文件或付费远程浏览器服务,直接调整代码即可适配:

适配后的代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument('--headless')
# 指定PythonAnywhere内置的Chrome二进制文件路径
options.binary_location = '/usr/bin/google-chrome'
# 无需手动指定chromedriver路径,会自动匹配内置版本
driver = webdriver.Chrome(options=options)
driver.get('URL TO SCRAPE')

必要的配置检查

  • 确认selenium已安装:如果环境中没有selenium,在PythonAnywhere的控制台执行 pip install selenium --user
  • 版本兼容问题:若遇到驱动与浏览器版本不匹配,可手动指定内置chromedriver路径:driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=options)(但优先推荐无路径的自动匹配方式)
  • 定时任务设置:在PythonAnywhere的Tasks面板配置定时运行规则,注意脚本的工作目录需与文件存放路径一致

关于远程浏览器服务的说明

BrowserStack、SauceLabs这类服务面向专业自动化测试场景,提供多浏览器、多系统的兼容性测试能力,对于简单的定时爬虫来说属于过度配置,且会产生不必要的成本,完全没有必要使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 20:27:07