selenium 无头模式 以及防止被检测

from selenium import webdriver
from selenium.webdriver.chrome.options import Options # => 引入Chrome的配置
import time

# 配置
ch_options = Options()
ch_options.add_argument("--headless")  # => 为Chrome配置无头模式

# 在启动浏览器时加入配置
driver = webdriver.Chrome(chrome_options=ch_options) # => 注意这里的参数

driver.get('http://baidu.com')
driver.find_element_by_id('kw').send_keys('测试')
driver.find_element_by_id('su').click()

time.sleep(2)

# 只有截图才能看到效果咯
driver.save_screenshot('./ch.png')

driver.quit()

 防止被检测设置:

from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
bro = Chrome(options=option)

url = "fudan.bbs.kaoyan.com"  # 首页
bro.get("http://fudan.bbs.kaoyan.com/")
bro.implicitly_wait(10)

 无头防止检测(添加一个请求头可以防止被检测)

from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument("user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36")

bro = Chrome(options=option)

url = "fudan.bbs.kaoyan.com"  # 首页
bro.get("http://fudan.bbs.kaoyan.com/")
bro.implicitly_wait(10)

 

posted @ 2020-04-25 16:07  凯帅  阅读(5204)  评论(0编辑  收藏  举报