在爬虫访问目标网站的过程中,很多网站为了避免被恶意访问,需要设置验证码登录,这样是为了避免非人类的访问。今天我们学习下如何使用python爬虫实现验证码登录并且将生成的验证码保存下来,然后人为输入后,包装后再post给服务器,实现验证,这里还涉及到了cookie和代理ip,cookie都是随机生成的。这里主要讲下代理ip的使用,因为不管网站是以什么方式进行验证,ip都是最基础的。因为目标网站一般都是从最基层的访问ip开始进行限制访问。
这里我们使用python简单的示例下,借鉴了www.16yun.cn他们家提供的demo:
// 要访问的目标页面 string targeturl = http://httpbin.org/ip; // 代理服务器(产品官网 www.16yun.cn) string proxyhost = http://t.16yun.cn; string proxyport = 31111; // 代理验证信息 string proxyuser = username; string proxypass = password; // 设置代理服务器 webproxy proxy = new webproxy(string.format({0}:{1}, proxyhost, proxyport), true); servicepointmanager.expect100continue = false; var request = webrequest.create(targeturl) as httpwebrequest; request.allowautoredirect = true; request.keepalive = true; request.method = get; request.proxy = proxy; //request.proxy.credentials = credentialcache.defaultcredentials; request.proxy.credentials = new system.net.networkcredential(proxyuser, proxypass); // 设置proxy tunnel // random ran=new random(); // int tunnel =ran.next(1,10000); // request.headers.add(proxy-tunnel, string.valueof(tunnel)); //request.timeout = 20000; //request.servicepoint.connectionlimit = 512; //request.useragent = mozilla/5.0 (windows nt 6.3; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/48.0.2564.82 safari/537.36; //request.headers.add(cache-control, max-age=0); //request.headers.add(dnt, 1); //string encoded = system.convert.tobase64string(system.text.encoding.getencoding(iso-8859-1).getbytes(proxyuser + : + proxypass)); //request.headers.add(proxy-authorization, basic + encoded); using (var response = request.getresponse() as httpwebresponse) using (var sr = new streamreader(response.getresponsestream(), encoding.utf8)) { string htmlstr = sr.readtoend();
demo里面主要是给大家示例了隧道代理的使用方式,是一种比较好用的代理方式。上述是小编给大家介绍的python爬虫实现验证码登录详解整合和代理的使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。
制造企业在“科技智造”这条路上不断开拓
解析UML类图和时序图
【安胜网络】共话安全与合规,企业数据合规培训班圆满举行!
C语言的简介和特点说明
七线电机怎么接线 七线电机接线电路图解
爬虫实现目标网站验证码登陆
LG Display广州建OLED生产线板上钉钉 韩方要求严禁技术泄露
新能源动力车BMS电池包PAK高压单芯连接器简介
深睿医疗发布行业首款单器官多病种全肺产品
微型TCA看起来将产生军事影响
利用迟滞降压转换器中的旁路模式增强PA性能和系统效率
赛灵思宣布扩展其UltraScale+产品组合,以支持新型应用
赛迪顾问重磅发布了《中国大数据产业白皮书及百强榜单》
连接器的拔插力试验
矩阵锁存器是如何工作的?
Lattice LCMXO2280 MachXO Mini评估开发方案
聚合物锂电安全使用指南
示波器常用术语名词解释
真无线耳机推荐-戴上索尼WF-1000XM4,耳中也有星辰大海
合肥大唐存储与KeyarchOS完成浪潮信息澎湃技术兼容性认证