selenium无界面执行和反爬

本文介绍如何使用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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值