欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

云打码的简单使用举例

程序员文章站 2022-04-03 08:51:34
...

1.在云打码官网上下载"Python调用示例"

具体步骤如下:

云打码的简单使用举例

云打码的简单使用举例

2.下载完成后会发现代码中会用到appid,appkey,那么问题就来了,它们是什么?又是从哪里获得的呢?

appid:即为下图中的软件代码;appkey:即为下图中的通讯**.

云打码的简单使用举例

云打码的简单使用举例

3.利用云打码获取验证码并进行登录及验证验证码是否输入正确

大概流程:请求验证码网址(获取到验证码图片)->下载验证码到本地->获取验证码图片-->登录--->识别结果

下面附上完整代码:

#ydm.py

import json, time, requests

# 此文件是从云打码中下载的
class YDMHttp(object):

    apiurl = 'http://api.yundama.com/api.php'
    username = ''
    password = ''
    #开发者id
    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 shibie(file_name):
    # 普通用户的用户名和密码
    username = ''
    password = ''

    # # 开发者id和**
    appid = ''
    appkey = ''

    filename = file_name
    # 验证码类型
    codetype = ''
    timeout = 60

    # 检查
    if (username == 'username'):
        print('请设置好相关参数再测试')
    else:
        yundama = YDMHttp(username, password, appid, appkey)
        cid, result = yundama.decode(filename, codetype, timeout);
        return result


if __name__ == '__main__':
    # 普通用户的用户名和密码
    username = ''
    password = ''

    # 开发者id和**
    appid = ''
    appkey = ''

    filename = 'getimage.jpg'
    # 验证码类型
    codetype = ''
    timeout = 60

    # 检查
    if (username == 'username'):
        print('请设置好相关参数再测试')
    else:
        yundama = YDMHttp(username, password, appid, appkey)
        cid, result = yundama.decode(filename, codetype, timeout);
        print('cid: %s, result: %s' % (cid, result))

#yundama.py

import requests
from fake_useragent import UserAgent
from http.cookiejar import LWPCookieJar
from ydm import shibie


session = requests.Session()
session.cookies = LWPCookieJar(filename='ydm.txt')
# 其中参数ignore_discard=True表示即使cookies将被丢弃也把它保存下来,它还有另外一个参数igonre_expires表示当前数据覆盖(overwritten)原文件。
try:
    session.cookies.load('ydm.txt', ignore_discard=True, ignore_expires=True)
    print('本地cookie加载完成')
except Exception as e:
    print('')

ua = UserAgent()
headers = {
    'headers': ua.random,
}

#获取验证码图片


def get_captcha_image():
    content = session.get('http://www.yundama.com/index/captcha', headers=headers).content
    with open('captcha.jpg', 'wb') as f:
        f.write(content)

#登录


def login():
    get_captcha_image()

    # 验证码保存到本地以后,对验证码进行识别
    result = shibie('captcha.jpg')

    print('识别结果:', result)
    #获取开发者登陆时请求的url
    response = session.get('http://www.yundama.com/index/login?username='开发者账号'&password='开发者密码'&utype=1&vcode={}'.format(result))

    print(response.text)

    session.cookies.save(ignore_discard=True, ignore_expires=True)


def get_index():
    response = session.get('http://www.yundama.com/user')
    print(response.text)


if __name__ == '__main__':
    login()
    get_index()

那么问题来了?如何获取开发者登录时请求的url呢?

可以使用抓包工具抓取云打码登录时请求的url

云打码的简单使用举例

4.运行yundama.pyde的结果如下

云打码的简单使用举例

那么你如何得知结果:到底验证码输入是否正确呢?

你只需打开cmd.exe,按照下图操作即可

云打码的简单使用举例