PythonPython

Python爬虫 | 云打码平台识别验证码

2020-05-12  本文已影响0人  生信师姐

相关的门户网站在进行登录的时候,如果用户连续登录的次数超过3次或者5次的时候,就会在登录页中动态生成验证码。通过验证码达到分流和反爬的效果。使用云打码平台识别验证码。

网站:http://www.yundama.com/

1.首次使用须知:

提分价格里可以看验证码类型

2.含验证码的网站请求流程:

3.实例
以请求人人网为例,来看看云达码是如何识别验证码的。

首先,把下载的示例脚本进行修改YunCode.py

import http.client, mimetypes, urllib, json, time, requests

######################################################################

class YDMHttp:

    apiurl = 'http://api.yundama.com/api.php'
    username = ''
    password = ''
    appid = ''
    appkey = ''

    def __init__(self, username, password, appid, appkey):
        self.username = username  
        self.password = password
        self.appid = str(appid)
        self.appkey = appkey

    def request(self, fields, files=[]):
        response = self.post_url(self.apiurl, fields, files)
        response = json.loads(response)
        return response
    
    def balance(self):
        data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['balance']
        else:
            return -9001
    
    def login(self):
        data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['uid']
        else:
            return -9001

    def upload(self, filename, codetype, timeout):
        data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
        file = {'file': filename}
        response = self.request(data, file)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['cid']
        else:
            return -9001

    def result(self, cid):
        data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid)}
        response = self.request(data)
        return response and response['text'] or ''

    def decode(self, filename, codetype, timeout):
        cid = self.upload(filename, codetype, timeout)
        if (cid > 0):
            for i in range(0, timeout):
                result = self.result(cid)
                if (result != ''):
                    return cid, result
                else:
                    time.sleep(1)
            return -3003, ''
        else:
            return cid, ''

    def report(self, cid):
        data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid), 'flag': '0'}
        response = self.request(data)
        if (response):
            return response['ret']
        else:
            return -9001

    def post_url(self, url, fields, files=[]):
        for key in files:
            files[key] = open(files[key], 'rb');
        res = requests.post(url, files=files, data=fields)
        return res.text

######################################################################
# 该函数是用来返回验证码图片显示的数据值
def getCodeText(codeType, imgPath):
    result = None  # 设置全局的result
    # 普通者用户名
    username = '------'

    # 普通者密码
    password = '----'

    # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
    appid = ---

    # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
    appkey = '-----'

    # 图片文件
    filename = imgPath

    # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html
    codetype = codeType

    # 超时时间,秒
    timeout = 30  # 超时时间timeout最好设置的长一点,否则因为网络不好,只设置一秒,就报错了。

    # 检查
    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)

        # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
        cid, result = yundama.decode(filename, codetype, timeout);
        print('cid: %s, result: %s' % (cid, result))
    return result
######################################################################

爬虫脚本
思想:

# 进行验证码的识别:将验证码进行本地下载,将其提交给打码平台进行识别
url = 'http://www.renren.com/'
page_text = requests.get(url=url, headers=headers).text
tree = etree.HTML(page_text)
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.jpg', 'wb') as fp:
    fp.write(code_img_data)

完整代码

import requests
from lxml import etree
from YunCode import *


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
}

# 进行验证码的识别:将验证码进行本地下载,将其提交给打码平台进行识别
url = 'http://www.renren.com/'
page_text = requests.get(url=url, headers=headers).text
tree = etree.HTML(page_text)
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.jpg', 'wb') as fp:
    fp.write(code_img_data)

# 将本地保存好的验证码图片交给打码平台识别
code_text = getCodeText(5000, './code.jpg')             # 注意这里的验证码类型,根据实际情况进行修改

# 模拟登录(发送post请求)
post_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019661134183'  # 不同时间的时间戳不一样的

data = {
    'email': 'xxxx@qq.com',
    'icode': code_text,
    'origURL': 'http://www.renren.com/home',
    'domain': 'renren.com',
    'key_id': '1',
    'captcha_type': 'web_login',
    'password': 'xxxxxxxxx',
    'rkey': '00313a9752665df609d455d36edfbe94',
    'f': '',
}
page_text = requests.post(url=post_url, headers=headers, data=data).text

with open('./renren.html', 'w', encoding='utf-8') as fp:
    fp.write(page_text)
上一篇下一篇

猜你喜欢

热点阅读