「Python3爬虫」使用云打码识别验证码
具体步骤如下:
1、首先是注册账号,然后进入这个网址(http://www.yundama.com/apidoc/YDM_SDK.html)选择PythonHTTP示例下载:
2、下载后解压,可以看到有如下几个文件,因为我使用的Python版本是3.5,所以打开YDMHTTPDemo3.x:
3、打开之后修改如下几个部分,用户名和密码就是你的用户名和密码,而appid和appkey需要进入开发者后台查看,第一次使用的时候还需要新建一个软件,才能有appid和appkey:
下图中的软件代码就是appid,通讯密钥就是appkey:
4、把信息都添加进去后运行代码,不出意外会返回一个1007,进入错误代码及排错(http://www.yundama.com/apidoc/YDM_ErrorCode.html)查找原因,原来是因为账户没有余额
然后进入用户后台充值就行了,充值完以后再次运行代码,就可以看到识别结果了。
有需要Python学习资料的小伙伴吗?小编整理【一套Python资料、源码和PDF】,感兴趣者可以关注小编后私信学习资料(是关注后私信哦)反正闲着也是闲着呢,不如学点东西啦
进行完如上步骤之后,我们就可以使用云打码平台来识别验证码了,不过为了使用方便,可以建一个YDMDemo.py,把账号密码等信息写进去,调用的时候只需要传入验证码图片就行了。
import json import time import 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 use_ydm(filename): username = '' # 用户名 password = '' # 密码 app_id = 1 # 软件ID app_key = '' # 软件密钥 code_type = 1004 # 验证码类型 timeout = 60 # 超时时间,秒 yundama = YDMHttp(username, password, app_id, app_key) # 初始化 balance = yundama.balance() # 查询余额 print('您的题分余额为{}'.format(balance)) cid, result = yundama.decode(filename, code_type, timeout) # 开始识别 print('识别结果为{}'.format(result)) return result
相关推荐
dhyddy 2015-12-10
蓝色深海 2020-09-15
songerxing 2020-06-12
CloudXli 2019-11-06
讨厌什么变成什么 2019-11-04
OwenJi 2019-11-02
Hannah 2019-05-10
chongtianfeiyu 2019-05-10
dalada 2019-04-11
82791897 2013-03-13
CloudXli 2019-06-30
Broadview 2019-06-28
GameBuilder 2012-05-09
qitong 2019-06-27
xufwind 2019-06-20
郭岚 2019-06-21
TangowL 2019-04-19
86347416 2019-01-02