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/')
  • 相关阅读:
    JavaScript表单验证年龄
    PHP会话处理相关函数介绍
    SpringCloud(第一天)
    springboot加强
    SpringBoot的第一个demo
    ElasticSearch(分布式全文搜索引擎)
    Redis集群
    NoSql和Redis
    ElementUI实现CRUD(修改前端页面),前后台解决跨域问题
    SSM+ElementUI综合练习和Swagger和postman的使用(第二天)
  • 原文地址:https://www.cnblogs.com/XLHIT/p/11317107.html
Copyright © 2011-2022 走看看