• 爬虫 无头浏览器 规避监测


    无头浏览器

    - phantomJs:无可视化界面的浏览器
    - 谷歌无头浏览器:
    from selenium.webdriver.chrome.options import Options。
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    browser = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)

    # 无头浏览器举例
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from time import sleep
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    
    bro = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options)
    
    bro.get('https://www.baidu.com')
    sleep(3)
    print(bro.page_source)
    bro.save_screenshot('1.png')
    
    bro.quit()

    规避监测

    现在不少大网站有对selenium采取了监测机制。比如正常情况下我们用浏览器访问淘宝等网站的 window.navigator.webdriver的值为 
    undefined。而使用selenium访问则该值为true。那么如何解决这个问题呢?
    
    只需要设置Chromedriver的启动参数即可解决问题。在启动Chromedriver之前,为Chrome开启实验性功能参数excludeSwitches,它的值为['enable-automation'],完整代码如下:
    
    from selenium.webdriver import Chrome
    from selenium.webdriver import ChromeOptions
    
    option = ChromeOptions()
    option.add_experimental_option('excludeSwitches', ['enable-automation'])
    driver = Chrome(options=option)

    - 相关的网站会对selenium发起的请求进行监测 - 网站后台可以根据window.navigator.webdriver返回值进行selenium的监测
    - undefinded:不是selenium进行的请求发送
    - true:是selenium发起的请求
    - 规避监测的方法:
    from selenium.webdriver import ChromeOptions
    option = ChromeOptions()
    option.add_experimental_option('excludeSwitches', ['enable-automation'])
    bro = webdriver.Chrome(executable_path='chromedriver.exe',options=option)

    from selenium import webdriver
    from selenium.webdriver import ChromeOptions
    option = ChromeOptions()
    option.add_experimental_option('excludeSwitches', ['enable-automation'])
    #实现了规避监测
    bro = webdriver.Chrome(executable_path='chromedriver.exe',options=option)
    bro.get('https://www.taobao.com/')
  • 相关阅读:
    Visual Studio Code插件安装步骤
    JS省城级联
    JS省城级联
    JS省城级联
    JS省城级联
    【JAVA零基础入门系列】Day9 Java中的那个大数值
    【JAVA零基础入门系列】Day9 Java中的那个大数值
    【JAVA零基础入门系列】Day9 Java中的那个大数值
    [js插件开发教程]实现一个比较完整的开源级选项卡插件
    VS2017桌面应用程序打包成.msi或者.exe
  • 原文地址:https://www.cnblogs.com/XLHIT/p/11317107.html
Copyright © 2020-2023  润新知