selenium 无界面模式下 反爬虫解决方案

在无界面模式下发现请求地址所有报404,在有界面的模式下就是正常运行的。在网上查了半天,发现这种状况,因为爬取的网站进行了selenium反爬虫致使的。linux

1.尝试使用开启开发者模式git

opt = webdriver.ChromeOptions()github

# 把chrome设置成无界面模式,不论windows仍是linux均可以,自动适配对应参数
opt.set_headless()
# 建立chrome无界面对象
opt.add_argument("--start-maximized") # 界面设置最大化
# opt.add_argument('no-sandbox')
opt.add_argument('--headless')
opt.add_argument('--disable-gpu')
opt.add_experimental_option('excludeSwitches', ['enable-automation'])#开启开发者模式
driver = webdriver.Chrome(options=opt)web

运行以后仍是会报404,这种方法放弃chrome

2.尝试使用firefix浏览器windows

opt=webdriver.FirefoxOptions()
opt.set_headless()
opt.add_argument("--start-maximized")
opt.add_argument('--headless')
opt.add_argument('--disable-gpu')
driver = webdriver.Firefox(options=opt)浏览器

成功解决问题,在无界面模式下能够爬取,less

https://github.com/mozilla/geckodriver/releases/ 该连接能够下载新的火狐浏览器驱动网站

相关文章
相关标签/搜索