我是靠谱客的博主 明亮黑米,这篇文章主要介绍selenium和puppeteer对阿里滑块验证进行操作和记录。第一种:selenium进行操作。第二种:puppeteer,现在分享给大家,希望可以做个参考。
第一种:selenium进行操作。
- 使用自带滑动和按键进行拖拽,会被检测到。大部分屏蔽方式加上也是过不去的。
- 使用selenium中driver的execute_script,Js注入操作。(不通过,不过这段代码在浏览器可正常操作)
复制代码
1
2
3
4
5
6js1 = "var slider = document.getElementById('nc_1_n1z'),container = slider.parentNode; " "var rect = slider.getBoundingClientRect(),x0= rect.x || rect.left,y0= rect.y || rect.top,w = container.getBoundingClientRect().width,x1= x0 + w,y1= y0; " "var mousedown = document.createEvent('MouseEvents'); mousedown.initMouseEvent('mousedown', true, true, window, 0, x0, y0, x0, y0, false, false, false, false, 0, null);slider.dispatchEvent(mousedown);" "var mousemove = document.createEvent('MouseEvents'); mousemove.initMouseEvent('mousemove', true, true, window, 0, x1, y1, x1, y1, false, false, false, false, 0, null); slider.dispatchEvent(mousemove);" driver.execute_script(js1)
- Windows可以用win32api 方式进行滑动。(可通过,局限比较大,点击代码自取)
- Pyautogui 可以在mac和windows进行。(服务器就不可用,点击代码自取)
第二种:puppeteer
- 直接使用puppeteer模块进行操作访问和存储,要比selemium几率大一些,但是还是很难通过。百分之10-20的概率。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136const puppeteer = require('puppeteer'); const fs = require('fs'); function update(url) { (async () => { // const browser = await puppeteer.launch(); const browser = await puppeteer.launch({ headless: true,slowMo: 150, args:['--window-size=1920,1080'], executablePath: '/Applications/Google Chrome.app/Contentas/MacOS/Google Chrome' })// 启动浏览器,传入headless为false可以打开窗口 // //启动新页面 const page = await browser.newPage(); // //设置页面打开时的页面宽度高度 await page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3477.0 Safari/537.36"); await page.setExtraHTTPHeaders({ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'Upgrade-Insecure-Requests': '1',}); //直接删掉webdriver属性 await page.evaluateOnNewDocument(() => { const newProto = navigator.__proto__; delete newProto.webdriver; navigator.__proto__ = newProto; }); //defineProperty删除webdriver后如何检测 await page.evaluateOnNewDocument(() => { Object.defineProperty(navigator, 'webdriver', { get: () => false, }); }); await page.evaluateOnNewDocument(() => { get: () => "Linux x86_64" }); //防止检测,赋值plugins await page.evaluateOnNewDocument(() => { Object.defineProperty(navigator, 'plugins', { get: () => [ { 0: {type: "application/x-google-chrome-pdf", suffixes: "pdf", description: "Portable Document Format", enabledPlugin: Plugin}, description: "Portable Document Format", filename: "internal-pdf-viewer", length: 1, name: "Chrome PDF Plugin" }, { 0: {type: "application/pdf", suffixes: "pdf", description: "", enabledPlugin: Plugin}, description: "", filename: "mhjfbmdgcfjbbpaeojofohoefgiehjai", length: 1, name: "Chrome PDF Viewer" }, { 0: {type: "application/x-nacl", suffixes: "", description: "Native Client Executable", enabledPlugin: Plugin}, 1: {type: "application/x-pnacl", suffixes: "", description: "Portable Native Client Executable", enabledPlugin: Plugin}, description: "", filename: "internal-nacl-plugin", length: 2, name: "Native Client" } ], }); }); //window.chrome绕开 await page.evaluateOnNewDocument(() => { window.chrome = { runtime: {}, loadTimes: function() {}, csi: function() {}, app: {} }; }); //早期绕开方式 function hasChrome() { return !navigator.language || !navigator.languages }; function hasChrome() { return !! window.chrome }; await page.goto(url); await page.waitForTimeout(5000) await page.evaluate( // setTimeout( function() { var slider = document.getElementById('nc_1_n1z'), container = slider.parentNode; var rect = slider.getBoundingClientRect(), x0 = rect.x || rect.left, y0 = rect.y || rect.top, w = container.getBoundingClientRect().width, x1 = x0 + w, y1 = y0; var mousedown = document.createEvent("MouseEvents"); mousedown.initMouseEvent("mousedown", true, true, window, 0, x0, y0, x0, y0, false, false, false, false, 0, null); slider.dispatchEvent(mousedown); var mousemove = document.createEvent("MouseEvents"); mousemove.initMouseEvent("mousemove", true, true, window, 0, x1, y1, x1, y1, false, false, false, false, 0, null); slider.dispatchEvent(mousemove); // }, 3000) }); await page.waitForTimeout(5000) //page.frames() 获取当前页面所有的 iframe,然后根据 iframe 的名字精确获取某个想要的 iframe const frame = await page.mainFrame(); const bodyHandle = await frame.$('html'); //获取所有的html //frame.evaluate()在浏览器中执行函数,相当于在控制台中执行函数,返回一个Promise const html = await frame.evaluate(body=>body.innerHTML,bodyHandle); await bodyHandle.dispose(); /* fs.wirteFile有三个参数 * 1,第一个参数是要写入的文件路径 * 2,第二个参数是要写入得内容 * 3,第三个参数是可选参数,表示要写入的文件编码格式,一般就不写,默认就行 * 4,第四个参数是个回调函数 只有一个参数error,来判断是否写入成功 */ browser.close(); fs.writeFile("a.html",html,error=>{ if(error) return console.log("写入文件失败,原因是:"+error.message); console.log('写入成功'); // process.kill(browser.process().pid); }); })(); }; update('https://www.123456789.com')//网站需要自己找下,如果用python调用会报错。
最后
以上就是明亮黑米最近收集整理的关于selenium和puppeteer对阿里滑块验证进行操作和记录。第一种:selenium进行操作。第二种:puppeteer的全部内容,更多相关selenium和puppeteer对阿里滑块验证进行操作和记录内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复