zoukankan      html  css  js  c++  java
  • 验证码处理+cookie模拟登录

    一、背景

    相关博文:https://www.jianshu.com/p/9fce799edf1e

    https://blog.csdn.net/h19910518/article/details/79348051

    Cookie

    ​ HTTP协议它是无状态的,就是说这一次请求和上一次请求是没有任何关系的,没有关联的。这种无状态的的好处是快速。但是有时我们希望几个请求的页面要有关联,比如:在a已经登录,在b也希望是登陆状态,但是,这是2个不同的页面,也就是2个不同的HTTP请求,这2个HTTP请求是无状态的,也就是无关联的,所以无法单纯的在b中读取到它在a中已经登陆了,使用数据库可以记录登录状态,但会给服务器造成压力。

    ​ Cookie指某些网站为了辨别用户身份,进行Session跟踪而存储在用户本地终端上的数据。当你浏览某网站时,网站存储在你机器上的一个小文本文件,它记录了你的用户ID,密码、浏览过的网页、停留的时间等信息,当你再次来到该网站时,cookie随每个请求发送到同一服务器,服务器通过读取Cookie,得知你的相关信息,就可以做出相应的动作。

    Session

    ​ Session:在计算机中,尤其是在网络应用中,称为“会话控制”。Session对象存储特定用户会话所需的属性及配置信息

    1.客户端发送一个 带有Set-Cookie 属性的请求;
    2.这个请求需要由服务端用session加密算法进行加密,得到一个session_id 和 cookie 的对应字典
    3.下次客户端登录时,浏览器会发送带有Cookie头部的请求的时候,用户就可以不用登陆了。
    

    存储在Session对象的变量不会丢失,而是在整个Session中一直存在下去。当用户请求来自应用程序的web页时,如果该用户还没有Session,则Web服务器会自动创建一个Session对象。而当Session过期或被放弃的时候,服务器会终止该Session。

    二、准备

    1.手动登录人人网

    • 查看验证码类型
    • 使用fiddler抓取数据(请求的url、cookie数据)

    2.云打码平台使用

    • 注册账号(用户和开发者)
    • 查看开发文档
    • 下载DLL

    三、主要步骤

    • 使用requests的get方法,获取验证码图片,保存至本地
    • 将本地的验证码图片上传至云打码进行识别
    • 将识别结果与其他数据(账号、密码等,通过fiddler抓取)进行封装到data参数中
    • 实例化一个Session对象,使用post方法,提交url和data参数,实现登录。

    代码

    import http.client, mimetypes, urllib, json, time, requests
    from lxml import etree
    from YDMHTTPDemo3.x import YDMHttp #将下载的DLL导入
    

    #给云打码定义一个函数
    def getVCode( username, password,filename,codeType): 
        appid = 'xxxx'
        appkey = '3b753c7c24fba02dexxxxxxxxxxxxxxx'
        filename = filename
        codeType = codeType
        timeout = 30
        if (username == 'username'):
            print('请设置好相关参数再测试')
        else:
            yundama = YDMHttp(username, password, appid, appkey)  #实现云打码用户登录
            uid = yundama.login();
            print('uid: %s' % uid)
            balance = yundama.balance();
            print('balance: %s' % balance)
            cid, result = yundama.decode(filename, codetype, timeout); #验证码图片上传,返回结果
            print('cid: %s, result: %s' % (cid, result))
    

    target1_url = "http://www.renren.com/"
    headers = headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
        }
    response = requests.get(url = target1_url,headers = headers)
    ht = response.text
    tree = etree.HTML(ht)
    img= tree.xpath('//img[@verifyPic_login]/@src')
    #data中的参数通过手动登录时,使用fiddler抓取。
    data = {             
            'captcha_type':'web_login',
            'domain':'renren.com',
            'email':'xxxxxxxx@xxx.com',  #邮箱
            'f':'',
            'icode':"",   #验证码
            'key_id':'1',
            'origURL':'http://www.renren.com/home',
            'password':'06735438342bxxxxxxxxxxxxxxxxxxxxxxxxx', #加密后的密码
            'rkey':'8a339012c2e46e9xxxxxxxxxxxxxxxxxx',
        }
    target2_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019841747473'
    if img:  #如果有验证码
        urllib.request.urlretrieve(img[0],'./getimage.jpg')
        VCode = getVCode( 'Sroxi', 'xxx', './getimage.jpg', '1006')
        print(VCode)
        data['icode'] = VCode
        
    session = requests.Session()
    session.post(url=target2_url,data = data,headers = headers)
    target3_url = 'http://www.renren.com/58xxxxxx'    
    response1 = session.get(url = target3_url,headers = headers)
    htmlfile = response1.text
    with open('renren.html','w',encoding = 'utf8') as f:
        f.write(htmlfile)
    print('finish')      
    
  • 相关阅读:
    PhpStorm6 创建yii framework项目
    RestSharp104.1反序化用法
    qt_plugin_instance: identifier not found 解决办法
    使用jsonlib2.1.jar报,org.apache.struts2.json.JSONWriter can not access a member of class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper with modifiers "public"转载
    win7下安装和卸载oracle 10g转载
    删除c,c++,java源文件中全部注释的Python脚本
    webkit允许跨域访问
    【JavaP6大纲】分布式事务篇:两阶段提交(2PC)
    【JavaP6大纲】功能设计篇:秒杀场景设计
    【JavaP6大纲】功能设计篇:库存超卖问题
  • 原文地址:https://www.cnblogs.com/notfind/p/11505022.html
Copyright © 2011-2022 走看看