原文: http://blog.gqylpy.com/gqy/360
云打码网址:http://www.yundama.com
云打码调用示例文件:http://www.yundama.com/apidoc/YDM_SDK.html#DLL
云打码调用错误代码及拍错:http://www.yundama.com/apidoc/YDM_ErrorCode.html
云打码验证码类型对应编号:http://www.yundama.com/price.html
首先,你必须有云打码账户:
如上图,账户分为
用户
和 开发者
,两个都要注册。 其次,你的云打码账户里还必须得有$_$
:
如上图,请登录
用户
,然后充$_$
。 最后,登录开发者
,添加软件:
好了,准备工作到这里就结束了。
现在,你可以去下载云打码调用示例文件,然后自己研究:
下载地址:http://www.yundama.com/apidoc/YDM_SDK.html#DLL
或者,直接使用下面整理好的接口(Python),直接调用即可:
import json import time import requests class YDMHttp: apiurl = 'http://api.yundama.com/api.php' # 使用你自己的云打码账户: username = 'zhuyongkang' # 你充钱的那个用户名 password = 'Qq137326237' # 密码 appid = '6572' # 软件代码,开发者分成必要参数。登录开发者后台【我的软件】获得! appkey = '2d3b2262fbe81a60bc42f6967eef89f1' # 通讯秘钥,开发者分成必要参数。登录开发者后台【我的软件】获得! def __init__(self, filename, codetype, timeout, username=None, password=None, appid=None, appkey=None): """ :param filename: 图片路径 :param codetype: 验证码类型,你一定要请参考:http://www.yundama.com/price.html :param timeout: 识别验证码时的超时时间(秒) """ self.filename = filename self.codetype = codetype self.timeout = timeout # 登录云打码 uid = self.login() # print(f'uid: {uid}') # 查询余额 balance = self.balance() # print(f'余额:{balance}') 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): cid = self.upload(self.filename, self.codetype, self.timeout) if (cid > 0): for i in range(0, self.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 if __name__ == '__main__': # 初始化 ydm = YDMHttp(filename='验证码图片路径', codetype='验证码类型', timeout='超时时间') # 开始识别图片验证码 cid, result = ydm.decode() print(f'cid: {cid}', ) print(f'识别结果:{result}')
实例:Python requests + 云打码识别图片验证码 模拟登录古诗文网
# 模拟登录古诗文网 import requests from lxml import etree from fake_useragent import UserAgent as ua from YDMHTTPDemo3 import YDMHttp # 导入上面整理好的云打码接口 # 古诗文网址 url = 'https://so.gushiwen.org' # 登录页面路径 login_page_url = '/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx' # 登录请求的url login_request_url = '/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx' # 请求头 headers = { 'User-Agent': ua(use_cache_server=False).random } # 准备一个session对象 session = requests.Session() # 获取登录页面内容 page_text = session.get(url + login_page_url, headers=headers).text # 获取保存图片验证码 tree = etree.HTML(page_text) code_img_url = url + tree.xpath('//*[@id="imgCode"]/@src')[0] img_data = session.get(code_img_url, headers=headers).content filename = 'code_img.jpg' with open(filename, 'wb') as fp: fp.write(img_data) # 使用云打码识别图片验证码 ydm = YDMHttp(filename=filename, codetype=1004, timeout=10) cid, code_text = ydm.decode() # print(f'识别结果:{code_text}') # 登录请求的参数 # 下面两个参数会变化,所以我们就动态获取: __VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0] __VIEWSTATEGENERATOR = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0] data = { '__VIEWSTATE': __VIEWSTATE, '__VIEWSTATEGENERATOR': __VIEWSTATEGENERATOR, 'from': 'http://so.gushiwen.org/user/collect.aspx', 'email': 'www.zhangbowudi@qq.com', 'pwd': 'bobo328410948', # 你没看错,它就是明文 'code': code_text, 'denglu': '登录', } # 开始模拟登录: page_text = session.post(url + login_request_url, headers=headers, data=data).text with open('gushiwen.html', 'w', encoding='utf-8') as fp: fp.write(page_text)