1、安装python、pip及程序用到的包
2、安装Chrome浏览器
3、安装Chromedriver,要与第二步安装的Chrome浏览器版本对应
Chromedriver 下载地址—70.0.3538.16-131.0.6778.85(持续更新中)_chromedriver 131.0.6778.86-优快云博客
4、开放对应端口
问题与解决:
1、WebDriver.__init__() got multiple values for argument 'options'
从Selenium 4.10.0版本开始,这个方法的参数发生了重要变化。主要的变化是,第一个参数不再是executable_path,而是options或service
from selenium.webdriver.chrome.service import Service service = Service(executable_path=r'/www/wwwroot/python/football/chromedriver') driver = webdriver.Chrome(service=service, options=chromeOptions)
2、加上不打开浏览器设置后,获取网页内容,提示无法访问
反向爬虫限制:一些网站可能会检测并阻止无头浏览器的访问,认为它们是自动化脚本。这可以通过修改Chrome选项来绕过,比如设置用户代理(user-agent)来模拟正常用户的访问
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36"
chromeOptions.add_argument(f'user-agent={user_agent}')
3、linux服务器,不是只安装ChromeDriver,还要安装Chrome浏览器,两个版本要一致
4、服务器上要放行端口,特别是云服务器,需要在策略或者防火墙里放行对应端口。
chromeOptions.add_argument('--remote-debugging-port=9222')
3万+

被折叠的 条评论
为什么被折叠?



