雲打碼實現處理驗證碼
處理驗證碼,我們需要藉助第三方平臺來幫我們處理,個人認為雲打碼處理驗證碼的準確度還是可以的
首先第一步,我們得先註冊一個雲打碼的賬號,普通使用者和開發者使用者都需要註冊一下
然後登陸普通使用者,登陸之後的介面是這樣的,
你需要有幾分才可以使用它.
第二步登陸開發者使用者:
然後點選開發文件
進入之後點選下載python相關的模組
下載之後我們解壓之後發現是裡面有三個檔案:
這裡以python3的程式碼為例:
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 ###################################################################### # 使用者名稱 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)) ###################################################################### 原裝程式碼
使用示例程式碼中的原始碼檔案中的程式碼進行修改,讓其識別驗證碼圖片中的資料值
#該函式就呼叫了打碼平臺的相關的介面對指定的驗證碼圖片進行識別,返回圖片上的資料值 def getCode(codeImg): # 雲打碼平臺普通使用者的使用者名稱 username = 'baijinshuo' # 雲打碼平臺普通使用者的密碼 password = 'bjs146531' # 軟體ID,開發者分成必要引數。登入開發者後臺【我的軟體】獲得! appid = 6003 # 軟體金鑰,開發者分成必要引數。登入開發者後臺【我的軟體】獲得! appkey = '1f4b564483ae5c907a1d34f8e2f2776c' # 驗證碼圖片檔案 filename = codeImg # 驗證碼型別,# 例:1004表示4位字母數字,不同型別收費不同。請準確填寫,否則影響識別率。在此查詢所有型別 http://www.yundama.com/price.html codetype = 3000 # 超時時間,秒 timeout = 20 # 檢查 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
import requests from lxml import etree import json import time import re #1.對攜帶驗證碼的頁面資料進行抓取 url = 'https://www.douban.com/accounts/login?source=movie' headers = { 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Mobile Safari/537.36' } page_text = requests.get(url=url,headers=headers).text #2.可以將頁面資料中驗證碼進行解析,驗證碼圖片下載到本地 tree = etree.HTML(page_text) codeImg_url = tree.xpath('//*[@id="captcha_image"]/@src')[0] #獲取了驗證碼圖片對應的二進位制資料值 code_img = requests.get(url=codeImg_url,headers=headers).content #獲取capture_id '<img id="captcha_image" src="https://www.douban.com/misc/captcha?id=AdC4WXGyiRuVJrP9q15mqIrt:en&size=s" alt="captcha" class="captcha_image">' c_id = re.findall('<img id="captcha_image".*?id=(.*?)&.*?>',page_text,re.S)[0] with open('./code.png','wb') as fp: fp.write(code_img) #獲得了驗證碼圖片上面的資料值 codeText = getCode('./code.png') print(codeText) #進行登入操作 post = 'https://accounts.douban.com/login' data = { "source": "movie", "redir": "https://movie.douban.com/", "form_email": "15027900535", "form_password": "bobo@15027900535", "captcha-solution":codeText, "captcha-id":c_id, "login": "登入", } print(c_id) login_text = requests.post(url=post,data=data,headers=headers).text with open('./login.html','w',encoding='utf-8') as fp: fp.write(login_text)