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

在无界面模式下发现请求地址全部报404,在有界面的模式下就是正常运行的。在网上查了半天,发现这种情况,由于爬取的网站进行了selenium反爬虫导致的。

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

opt = webdriver.ChromeOptions()

# 把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)

运行之后还是会报404,这种方法放弃

2.尝试使用firefix浏览器

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)

成功解决问题,在无界面模式下可以爬取,

https://github.com/mozilla/geckodriver/releases/ 该链接可以下载新的火狐浏览器驱动



猜你喜欢

转载自www.cnblogs.com/sophia201552/p/11764643.html