• Python+Request库+第三方平台实现验证码识别示例


    1、登录时经常的出现验证码,此次结合Python+Request+第三方验证码识别平台(超级鹰识别平台)

    2、首先到超级鹰平台下载对应语言的识别码封装,超级鹰平台:http://www.chaojiying.com/price.html

    3、使用超级鹰平台需进行注册,购买1元,应该有1000次积分,再生产对应软件ID

    4、超级鹰下载Python语言进行Demo示例,代码如下(命名为ClassCode.py):

    import requests
    from hashlib import md5

    class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
    self.username = username
    password = password.encode('utf8')
    self.password = md5(password).hexdigest()
    self.soft_id = soft_id
    self.base_params = {
    'user': self.username,
    'pass2': self.password,
    'softid': self.soft_id,
    }
    self.headers = {
    'Connection': 'Keep-Alive',
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
    }

    def PostPic(self, im, codetype):
    """
    im: 图片字节
    codetype: 题目类型 参考 http://www.chaojiying.com/price.html
    """
    params = {
    'codetype': codetype,
    }
    params.update(self.base_params)
    files = {'userfile': ('ccc.jpg', im)}
    r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
    return r.json()

    def ReportError(self, im_id):
    """
    im_id:报错题目的图片ID
    """
    params = {
    'id': im_id,
    }
    params.update(self.base_params)
    r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
    return r.json()

    5、下面使用ClassCode.py模块中的代码,对人人网实现验证码登录,此时示例分别使用Cookie和Session进行演示
    import requests
    from .ClassCode import Chaojiying_Client (导入ClassCode.py 模块)
    from lxml import etree (需安装:pip3 install lxml)

    # 对验证码图片进行抓捕及识别
    url = 'http://www.renren.com/SysHome.do'

    headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36",
    }
    page_text = requests.get(url=url,headers=headers).text
    tree = etree.HTML(page_text)
    try:
    code_img_src = tree.xpath('//*[@id="verifyPic_login"]/@src')[0]
    code_img_data = requests.get(url=code_img_src, headers=headers).content
    with open('./code_img_data.jpg', 'wb') as fp:
    fp.write(code_img_data)
    except Exception as msg:
    print('元素不存在')

    # 使用超级鹰平台实现验证码识别
    chaojiying = Chaojiying_Client('超级鹰登录账号', '超级鹰登录密码', '根据验证码类型')
    with open('code_img_data.jpg', 'rb') as fp:
    img = fp.read()
    result = chaojiying.PostPic(img, 1005)['pic_str']
    print(result)

    ################################################
    使用Cookie进行登录
    ################################################
    # 使用Post发送登录请求 -- 使用Cookie进行登录
    def login_url():
    login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2021212242112'
    data = {
    "email": "人人网登录账号",
    "password": "人人网登录密码",
    "icode": result,
    "origURL": "http://www.renren.com/home",
    "domain": "renren.com",
    "key_id": 1,
    "captcha_type": "web_login",
    "f":""
    }
    response = requests.post(url=login_url,headers=headers,data=data)
    print(response.status_code)
    login_page_text = response.text
    with open('renren.html','w',encoding='utf-8') as fp:
    fp.write(login_page_text)
    return response.cookies

    def Profile():
    pro_url = 'http://www.renren.com/969120248/profile'
    pr_respose_text = requests.get(url=pro_url,headers=headers,cookies=login_url()).text
    with open('profile.html','w',encoding='utf-8') as fp:
    fp.write(pr_respose_text)

    Profile()

    ################################################
    使用Session进行登录
    ################################################
    # 使用Post发送登录请求 -- 使用Session进行登录
    # 创建session对象
    session = requests.Session()

    def login_url_Se():
    login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2021212242112'
    data = {
    "email": "人人网登录账号",
    "password": "人人网登录密码",
    "icode": result,
    "origURL": "http://www.renren.com/home",
    "domain": "renren.com",
    "key_id": 1,
    "captcha_type": "web_login",
    "f":""
    }
    response = session.post(url=login_url,headers=headers,data=data)
    print(response.status_code)
    login_page_text = response.text
    with open('renren.html','w',encoding='utf-8') as fp:
    fp.write(login_page_text)
    return response.cookies

    def Profile_Se():
    pro_url = 'http://www.renren.com/969120248/profile'
    pr_respose_text = session.get(url=pro_url,headers=headers,cookies=login_url_Se()).text
    with open('profile_New.html','w',encoding='utf-8') as fp:
    fp.write(pr_respose_text)

    Profile_Se()
  • 相关阅读:
    Android学习之简单的数据存储
    Android学习Tabhost、gallery、listview、imageswitcher
    Android学习之菜单
    Android学习之Notification
    android自定义控件
    Android简单登录系统
    Android学习自定义Dialog
    Android学习之DialogFragment
    Intent组件
    git
  • 原文地址:https://www.cnblogs.com/Teachertao/p/14675172.html
Copyright © 2020-2023  润新知