您的位置:首页 > 其它

selenium爬虫被检测到 该如何破?

2019-08-18 22:43 2651 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/Python1996/article/details/99709167

selenium爬虫被检测到 该如何破?

如何获取cookies?
使用的方法是通过 mitmproxy 蔽掉识别 webdriver 标识符的 js 文件。
首先下载mitproxy,pip安装方法: pip install mitmproxy基本使用方法:给本机设置代理ip 127.0.0.1端口8001(为了让所有流量走mitmproxy)具体方法请百度。

  1. 启动mitmproxy。windows:mitmdump -p 8001Linux:mitmproxy -p 8001
  2. 打开chrome的开发者工具,找到目标网站是通过哪个js文件控制webdriver相应的

我们发现,是yoda.xxx.js文件在控制webdriver的请求与响应。
4. 开始写干扰脚本(DriverPass.py):

import re
from mitmproxy import ctx

def response(flow):
if '/js/yoda.' in flow.request.url:
for webdriver_key in ['webdriver', '__driver_evaluate', '__webdriver_evaluate', '__selenium_evaluate', '__fxdriver_evaluate', '__driver_unwrapped', '__webdriver_unwrapped', '__selenium_unwrapped', '__fxdriver_unwrapped', '_Selenium_IDE_Recorder', '_selenium', 'calledSelenium', '_WEBDRIVER_ELEM_CACHE', 'ChromeDriverw', 'driver-evaluate', 'webdriver-evaluate', 'selenium-evaluate', 'webdriverCommand', 'webdriver-evaluate-response', '__webdriverFunc', '__webdriver_script_fn', '__$webdriverAsyncExecutor', '__lastWatirAlert', '__lastWatirConfirm', '__lastWatirPrompt', '$chrome_asyncScriptInfo', '$cdc_asdjflasutopfhvcZLmcfl_' ]:
ctx.log.info('Remove "{}" from {}.'.format(
webdriver_key, flow.request.url
))
flow.response.text = flow.response.text.replace('"{}"'.format(webdriver_key), '"NO-SUCH-ATTR"')
flow.response.text = flow.response.text.replace('t.webdriver', 'false')
flow.response.text = flow.response.text.replace('ChromeDriver', '')
  1. 退出刚才的mitmproxy状态,重新用命令行启动mitmproxy干扰脚本 监听8001端口的请求与响应。mitmdump -s DriverPass.py -p 8001
  2. 现在别管mitmproxy,启动webdriver 顺利获得cookies。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: