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/')
  • 相关阅读:
    make输出全部重定向到文件
    python selenium-webdriver 元素定位(三)
    通过vmware 启动cloudera-quickstart-vm-5.10.0-0-vmware.zip镜像无法启动。
    编写第一个python selenium-webdriver程序(二)
    sublime Text3 新建文件时定义模块
    python selenium-webdriver 环境搭建(一)
    gitlab 添加SSH Key
    python 使用 'python -m pip install --upgrade pip'提示PermissionError: [WinError 5] 拒绝访问
    bitnami gitlab 配置域名
    bitnami gitlab 安装
  • 原文地址:https://www.cnblogs.com/XLHIT/p/11317107.html
Copyright © 2011-2022 走看看