處理驗證碼,最通用的方式就是使用三方平臺幫我們對相關的驗證碼進行識別和處理。其中云打碼平臺就是一個非常通用的打碼平臺。
http://www.yundama.com/demo.html
該平臺可以幫助我們識別的驗證碼有如下類型:
使用流程:
云打碼平臺:
在官網中進行注冊(普通用戶和開發者用戶)
登錄開發者用戶:
實例代碼的下載(開發文檔-》調用示例及最新的DLL-》PythonHTTP實例下載)
創建一個軟件:我的軟件-》添加新的軟件
使用示例代碼中的源碼文件中的代碼進行修改,讓其識別驗證碼圖片中的數據值
代碼如下:
平臺提供的類:
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 = passwordself.appid = str(appid)self.appkey = appkeydef request(self, fields, files=[]):response = self.post_url(self.apiurl, fields, files)response = json.loads(response)return responsedef 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 -9001def 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 -9001def 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 -9001def 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, resultelse: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 -9001def 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
平臺提供的調用程序:
######################################################################
# 用戶名
username = 'username'
# 密碼
password = 'password'
# 軟件ID,開發者分成必要參數。登錄開發者后臺【我的軟件】獲得!
appid = 1
# 軟件密鑰,開發者分成必要參數。登錄開發者后臺【我的軟件】獲得!
appkey = '22cc5376925e9387a23cf797cb9ba745'
# 圖片文件
filename = 'getimage.jpg'
# 驗證碼類型,# 例:1004表示4位字母數字,不同類型收費不同。請準確填寫,否則影響識別率。在此查詢所有類型 http://www.yundama.com/price.html
codetype = 1004
# 超時時間,秒
timeout = 60
# 檢查
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))
######################################################################
注意:使用該平臺識別驗證碼,必須將驗證碼事先下載到本地,然后再將本地存儲的驗證碼提交給平臺的示例程序進行識別操作。
總結
以上是生活随笔為你收集整理的python爬虫(云打码平台)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。