当前位置:网站首页>Selenium 搭建 Cookies池 绕过验证反爬登录
Selenium 搭建 Cookies池 绕过验证反爬登录
2022-06-26 08:30:00 【冯大少】
在之前的博客介绍了,当今互联网开发者为减少各种爬虫对服务器的影响,采取了各种的反爬机制。最近有人提出了一个问题,不会验证码的算法破解, 怎样去完成 Selenium 全自动登录呢?其实既然有反爬机制,就肯定有每种的破解方法,只是每种的复杂程度不一样而已。今天就介绍一种比较直接的方法,通过搭建 Cookies池去绕过验证这一步。
Cookie 是浏览器储存在用户电脑上的一小段文本文件。该文件里存了加密后的用户信息(User ID, Password),过期时间(Expired Date/Time)等等。
因为 Cookie 有过期时间,图1,t 由于Cookies 涉及 User/Password,这里就遮挡部分编码。 当失效时,需要再次重新登录,生成新的 Cookie。简单理解,它就像一张港澳通行证,过了签证有效期,就必须重新签才可以用。
因此我们必须先完成一次手动的正常登录,系统自动生成Cookies, 然后通过 response.cookies() 方法去直接获取,当然,如果不熟悉通过代码去快速提取,也可以手动去复制粘贴,比较耗时。
通过获取的Cookies后,首先添加一个空 lst,然后把多个Cookies去组成一个独立的 dict,通过循环去添加到这个空 lst里,组成了一个Cookies池,图2
快速搭建了Cookies池后,当我们打开相对应的 URL 后,就可以让浏览器通过循环自动逐一添加Cookie,添加完成后,把浏览器刷新一下,代码入下:
需要注意的事,add_cookie()方法的 params 必须包含 “Name” 和 " Value",否则会报错。所以在创建的独立 Cookie, 格式必须是 {‘name’:‘xxx’, ‘value’:‘xxxx’}, 所以可以理解为 name 就相当于 Cookie 的 Key,value 就相当于 value ( Key_Value 键值对) 。
最后,创建一个类,把Selenium的属性设置好,
把Cookies池,添加的方法全部封装在一个自定义的函数里,运行一下,就可以达到自动登录的效果。最后提醒一下,相关的 Cookies 不要给别人,代码程序必须加密保管好,因为相关的 User/Password 有被反编译的风险而造成被盗,这里就不介绍怎样把编译的 Cookie 还原真实的信息。
for i in cookies:
browser.add_cookie(i)
browser.refresh()
图1

图2

边栏推荐
- nn. Modulelist and nn Sequential
- Koa_ mySQL_ Integration of TS
- Simulation of parallel structure using webots
- RF filter
- Degree of freedom analysis_ nanyangjx
- Opencv learning notes II
- OpenGL display mat image
- Detailed explanation of SOC multi-core startup process
- Matlab function foundation (directly abandon version)
- Implementation of ffmpeg audio and video player
猜你喜欢

Opencv learning notes 3

opencv學習筆記三

Koa_mySQL_Ts 的整合

HEVC学习之码流分析

static const与static constexpr的类内数据成员初始化

WBC learning notes (I): manually push WBC formula

STM32 project design: temperature, humidity and air quality alarm, sharing source code and PCB

Playing card image segmentation

(1) Turn on the LED

Esp8266wifi module tutorial: punctual atom atk-esp8266 for network communication, single chip microcomputer and computer, single chip microcomputer and mobile phone to send data
随机推荐
Two ways to realize time format printing
Swift code implements method calls
Transformers loading Roberta to implement sequence annotation task
Convex optimization of quadruped
51 MCU project design: Based on 51 MCU clock perpetual calendar
The principle and function of focus
Monitor iPad Keyboard Display and hide events
Fabrication of modulation and demodulation circuit
Relation extraction model -- spit model
Leetcode22 summary of types of questions brushing in 2002 (XII) and collection search
Addition of attention function in yolov5
RecyclerView Item 根据 x,y 坐标得到当前position(位置)
pgsql_ UDF01_ jx
Using transformers of hugging face to realize multi label text classification
(1) Turn on the LED
Partial summary of 45 lectures on geek time MySQL
Line detection_ nanyangjx
Exploration of webots and ROS joint simulation (I): software installation
Realizing sequence annotation with transformers
Relationship extraction --tplinker