当前位置:   article > 正文

爬虫selenium:unexpected keyword argument ‘options‘ & use options instead of chrome_options_typeerror: __init__() got an unexpected keyword ar

typeerror: __init__() got an unexpected keyword argument 'chrome_options

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

  1. from selenium import webdriver
  2. from time import sleep
  3. from selenium.webdriver.chrome.options import Options
  4. from selenium.webdriver import ChromeOptions
  5. # non visual interface
  6. chrome_options = Options()
  7. chrome_options.add_argument('--headless')
  8. chrome_options.add_argument('--disable-gpu')
  9. # avoid detection risks
  10. option = ChromeOptions()
  11. option.add_experimental_option('excludeSwitches', ['enable-automation'])
  12. driver = webdriver.Chrome(executable_path='./chromedriver.exe', chrome_options=chrome_options, options=option)
  13. driver.get('https://www.baidu.com')
  14. # get page source
  15. print(driver.page_source)
  16. sleep(2)
  17. 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')

解决方式 

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.service import Service
  3. # 创建一个Service对象,指定ChromeDriver的路径
  4. service = Service('./chromedriver.exe')
  5. # 通过Service对象来初始化Chrome WebDriver
  6. 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对象,如下

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

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

 最终代码

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

期待指点...

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/445843
推荐阅读
相关标签
  

闽ICP备14008679号