selenium无界面执行和反爬

selenium无界面执行和反爬

无界面执行

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

url="https://www.baidu.com"
chrome_options=Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")

chrome=webdriver.Chrome(executable_path="chromedriver",chrome_options=chrome_options)

chrome.get(url)
print(chrome.page_source)

chrome.quit()

规避服务端发现selenium请求的风险

from selenium import webdriver
#实现无可视化界面
from selenium.webdriver.chrome.options import Options
#实现规避检测
from selenium.webdriver import ChromeOptions

url="https://www.baidu.com"

#无可视化
chrome_options=Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
#规避检测
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])

chrome=webdriver.Chrome(executable_path="chromedriver",chrome_options=chrome_options,options=option)

chrome.get(url)
print(chrome.page_source)

chrome.quit()
原文地址:https://www.cnblogs.com/zx125/p/11487665.html