zoukankan      html  css  js  c++  java
  • selenium无头浏览器&规避操作

     一、无头浏览器概述: 无头浏览器主要目的是打开浏览器但用户看不到

     简单用法如下:

    from selenium import webdriver
    from time import sleep
    from selenium.webdriver.chrome.options import Options
    
    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(2)
    print(bro.page_source)
    bro.save_screenshot('1.png') #截图,后缀必须是png
    sleep(2)
    bro.quit()

    二、规避操作

      规避造作主要存在的意义把selenium程序伪装成一个正常的请求,欺骗浏览器

      正常情况下通过浏览器打开的一个网页的,在其console下执行window.navigator.webdriver会返回undefind,而通过selenium打开的浏览器下执行window.navigator.webdriver返回的是true,所以此处就需要用到规避。

    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')
  • 相关阅读:
    lombok注解详细介绍
    基于注解的SpringMVC大致开发流程
    Thymeleaf知识点总结
    thymeleaf相关知识
    Windows 命令行查看占用端口,并关闭操作
    身份证号码的正则表达式及验证详解
    mysql的查询过程和SQL语句优化
    MySQL数据库知识点整理
    MySQL数据库
    MySQL数据库
  • 原文地址:https://www.cnblogs.com/guniang/p/11727492.html
Copyright © 2011-2022 走看看