zoukankan      html  css  js  c++  java
  • 爬虫 无头浏览器 规避监测

    无头浏览器

    - 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/')
  • 相关阅读:
    jupytr notebook远程登录Linux服务器,切换conda虚拟环境
    Linux 命令su 和 su -执行机制的区别
    Shell编程知识4-su详细
    Shell编程知识3
    Shell编程知识2
    Ubuntu # echo $PATH //查看当前用户的环境变量--》修改配置环境变量
    Util和Helper类
    Qt QBarSeries简易柱状图教程
    解决QIcon引用qrc不显示图片
    qt调用quit()后未结束线程解决方案
  • 原文地址:https://www.cnblogs.com/XLHIT/p/11317107.html
Copyright © 2011-2022 走看看