爬虫selenium:unexpected keyword argument ‘options‘ & use options instead of chrome

03-11 6551阅读 0评论

在学习Python超强爬虫8天速成(完整版)爬取各种网站数据实战案例Day7 - 06.无头浏览器+规避检测时候老师演示的代码,遇到一些问题及解决过程,供分享和指点

爬虫selenium:unexpected keyword argument ‘options‘ & use options instead of chrome 第1张
(图片来源网络,侵删)
from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ChromeOptions
# non visual interface
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# avoid detection risks
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = webdriver.Chrome(executable_path='./chromedriver.exe', chrome_options=chrome_options, options=option)
driver.get('https://www.baidu.com')
# get page source
print(driver.page_source)
sleep(2)
driver.quit()

由于刚开始使用的是seleniumV3.7报错TypeError: __init__() got an unexpected keyword argument 'options' ,作为初学者,比较疑惑,网上没有找到合适的解决办法,尝试将selenium升级到Version4.1.0,但是会有两个warning,

01: DeprecationWarning: executable_path has been deprecated, please pass in a Service object  发生于driver = webdriver.Chrome(executable_path='./chromedriver.exe')

解决方式 

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# 创建一个Service对象,指定ChromeDriver的路径
service = Service('./chromedriver.exe')
# 通过Service对象来初始化Chrome WebDriver
driver = webdriver.Chrome(service=service)

 02:DeprecationWarning: use options instead of chrome_options 发生于driver = webdriver.Chrome(service=service, chrome_options=chrome_options, options=option),

但是chrome_options和option都需要传入options,不知如何解决,但是最后尝试将无界面和反检测相应配置参数都传入Options对象,如下

爬虫selenium:unexpected keyword argument ‘options‘ & use options instead of chrome 第2张
(图片来源网络,侵删)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# 创建一个Service对象,指定ChromeDriver的路径
service = Service('./chromedriver.exe')
# 通过Service对象来初始化Chrome WebDriver
driver = webdriver.Chrome(service=service)

经过测试,后台运行和防止被检测均生效

 最终代码

from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
chrome_options = Options()
# non visual interface
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# avoid detection risks
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 创建一个Service对象,指定ChromeDriver的路径
service = Service('./chromedriver.exe')
# 通过Service对象来初始化Chrome WebDriver
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get('https://www.baidu.com')
print(driver.page_source)
sleep(2)
driver.quit()

期待指点...


免责声明
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明。
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所
提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何
损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在
转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并白负版权等法律责任。

手机扫描二维码访问

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,6551人围观)

还没有评论,来说两句吧...

目录[+]