欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用 mitmdump 进行 selenium webDriver绕过网站反爬服务的方法 pdd某宝 可用

程序员文章站 2024-02-01 10:43:10
安装: pip install mitmproxy 新建一个脚本 脚本代码: 1 from mitmproxy import ctx 2 3 injected_javascript = ''' 4 // overwrite the `languages` property to use a cust ......

安装:  pip install  mitmproxy

新建一个脚本

脚本代码:

 1 from mitmproxy import ctx
 2 
 3 injected_javascript = '''
 4 // overwrite the `languages` property to use a custom getter
 5 object.defineproperty(navigator, "languages", {
 6   get: function() {
 7     return ["zh-cn","zh","zh-tw","en-us","en"];
 8   }
 9 });
10 // overwrite the `plugins` property to use a custom getter.
11 object.defineproperty(navigator, 'plugins', {
12   get: () => [1, 2, 3, 4, 5],
13 });
14 // pass the webdriver test
15 object.defineproperty(navigator, 'webdriver', {
16   get: () => false,
17 });
18 // pass the chrome test.
19 // we can mock this in as much depth as we need for the test.
20 window.navigator.chrome = {
21   runtime: {},
22   // etc.
23 };
24 // pass the permissions test.
25 const originalquery = window.navigator.permissions.query;
26 window.navigator.permissions.query = (parameters) => (
27   parameters.name === 'notifications' ?
28     promise.resolve({ state: notification.permission }) :
29     originalquery(parameters)
30 );
31 '''
32  
33 def response(flow):
34     # only process 200 responses of html content.
35     if not flow.response.status_code == 200:
36         return
37  
38     # inject a script tag containing the javascript.
39     html = flow.response.text
40     html = html.replace('<head>', '<head><script>%s</script>' % injected_javascript)
41     flow.response.text = str(html)
42     ctx.log.info('插入成功 ')
43  

然后运行mitmdump 

mitmdump -s xxxx.py 

默认为8080端口,将webdriver设置代理127.0.0.1:8080

然后就能监听和修改selenium特征了。

上一篇: 对Python日期的加减等操作

下一篇: