驗證碼處理:
雲打碼:http://www.yundama.com/index/reg
完整代碼:https://github.com/gaofengming1115/Test/tree/master/pachong_request_yundama/%E9%AA%8C%E8%AF%81%E7%A0%81
- 1.手動識別驗證碼
- 2.雲打碼平台自動識別驗證碼
雲打碼平台處理驗證碼的實現流程:
- 1.對攜帶驗證碼的頁面數據進行抓取
- 2.可以將頁面數據中驗證碼進行解析,驗證碼圖片下載到本地
- 3.可以將驗證碼圖片提交給三方平台進行識別,返回驗證碼圖片上的數據值
- 雲打碼平台:
- 1.在官網中進行注冊(普通用戶和開發者用戶)
- 2.登錄開發者用戶:
- 1.實例代碼的下載(開發文檔-》調用實例及最新的DLL-》PythonHTTP實例下載)
- 2.創建一個軟件:我的軟件-》添加新的軟件
-3.使用示例代碼中的源碼文件中的代碼進行修改,讓其識別驗證碼圖片中的數據值
雲打碼普通用戶
#該函數就調用了打碼平台的相關的接口對指定的驗證碼圖片進行識別,返回圖片上的數據值 def getCode(codeImg): # 雲打碼平台普通用戶的用戶名 username = '用戶' # 雲打碼平台普通用戶的密碼 password = '密碼' # 軟件ID,開發者分成必要參數。登錄開發者后台【我的軟件】獲得! appid = 6003 # 軟件密鑰,開發者分成必要參數。登錄開發者后台【我的軟件】獲得! appkey = 'xxxxxxxxxxx' # 驗證碼圖片文件 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": "用戶名", "form_password": "用戶密碼", "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)
登錄功能代碼自己示例
import request from lxml import etree import json # 1-對攜帶驗證碼的頁面數據進行抓取 url='https://epass.icbc.com.cn/regist/regist_index.jsp?StructCode=1' agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' headers = { 'User-Agent': agent, } proxy={ "http":"121.8.98.196:80", } page_text = request.get(url=url,proxies=proxy,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 # 獲取captcha-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] # 獲取img中id的值 with open('./code.png', 'wb') as fp: fp.write(code_img) # 獲得了驗證碼圖片上面的數據值 codeText = getCode('./code.png') # 進行登錄操作 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':'登錄', } 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)
雲打碼py3
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