十七、打码平台的使用
生活随笔
收集整理的這篇文章主要介紹了
十七、打码平台的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、常見的打碼平臺
云打碼:http://www.yundama.com/ (能夠解決通用的驗證碼識別)
極驗驗證碼智能識別輔助:http://jiyandoc.c2567.com/ (能夠解決復雜驗證碼的識別)
2、云打碼的使用
2.1 云打碼官方接口
下面代碼是云打碼平臺提供,做了個簡單修改,實現了兩個方法:
- indetify:傳入圖片的響應二進制數即可
- indetify_by_filepath:傳入圖片的路徑即可識別
其中需要自己配置的地方是:
username = 'whoarewe' # 用戶名password = '***' # 密碼appid = 4283 # appidappkey = '02074c64f0d0bb9efb2df455537b01c3' # appkeycodetype = 1004 # 驗證碼類型云打碼官方提供的api如下:
# THE WINTER IS COMING! the old driver will be driving who was a man of the world! # -*- coding: utf-8 -*- python 3.6.7, create time is 18-12-13 上午9:33 GMT+8# coding:utf-8import requests import json import timeclass YDMHttp:apiurl = 'http://api.yundama.com/api.php'username = ''password = ''appid = ''appkey = ''def __init__(self, username, password, appid, appkey):self.username = usernameself.password = passwordself.appid = str(appid)self.appkey = appkeydef request(self, fields, files=[]):print(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}print(file)response = self.request(data, file)print(response)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 post_url(self, url, fields, files=[]):for key in files:files[key] = open(files[key], 'rb')res = requests.post(url, files=files, data=fields)# print(res.request.__dict__)return res.textappid = 4283 # appidappkey = '02074c64f0d0bb9efb2df455537b01c3' # appkeyfilename = 'b.jpg' # 文件位置codetype = 1004 # 驗證碼類型# 超時 timeout = 60def indetify(response_content):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(response_content, codetype, timeout)print('cid: %s, result: %s' % (cid, result))return resultdef indetify_by_filepath(file_path):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(file_path, codetype, timeout)print('cid: %s, result: %s' % (cid, result))return resultif __name__ == '__main__':indetify_by_filepath('b.jpg')2.2 代碼中調用云打碼的接口
下面以豆瓣登錄過程中的驗證碼為例,了解云打碼如何使用
# coding=utf-8 from selenium import webdriver import time import requests from yundama import indetifydriver = webdriver.Chrome()driver.get("https://www.douban.com/")#輸入用戶名 driver.find_element_by_id("form_email").send_keys("78****@qq.com")#輸入密碼 driver.find_element_by_id("form_password").send_keys("****")#獲取驗證碼的地址 img_url = driver.find_element_by_id("captcha_image").get_attribute("src") response = requests.get(img_url) #請求驗證碼的地址 ret = indetify(response.content) #驗證碼識別#輸入驗證碼 driver.find_element_by_id("captcha_field").send_keys(ret)time.sleep(5) #點擊登錄 driver.find_element_by_class_name("bn-submit").click()time.sleep(10) print(driver.get_cookies()) driver.quit()2.3 若快打碼識別
import hashlib import requests from datetime import datetimeRUOUSER = '' RUOPASS = ''# 若快 12306打碼 直接傳入本地文件路徑 def getCode(img):url = "http://api.ruokuai.com/create.json"fileBytes = open(img, "rb").read()paramDict = {'username': RUOUSER,'password': RUOPASS,'typeid': 6113, # 專門用來識別12306圖片驗證的類型id'timeout': 90,'softid': 117157, # 推廣用的'softkey': '70acaa1e477a4374a7736264a24b974b' # 推廣用的}paramKeys = ['username','password','typeid','timeout','softid','softkey']result = http_upload_image(url, paramKeys, paramDict, fileBytes)return result['Result']# 若快12306打碼 上傳圖片 def http_upload_image(url, paramKeys, paramDict, filebytes):timestr = datetime.now().strftime('%Y-%m-%d %H:%M:%S')boundary = '------------' + hashlib.md5(timestr.encode("utf8")).hexdigest().lower()boundarystr = '\r\n--%s\r\n' % (boundary)bs = b''for key in paramKeys:bs = bs + boundarystr.encode('ascii')param = "Content-Disposition: form-data; name=\"%s\"\r\n\r\n%s" % (key, paramDict[key])# print parambs = bs + param.encode('utf8')bs = bs + boundarystr.encode('ascii')header = 'Content-Disposition: form-data; name=\"image\"; filename=\"%s\"\r\nContent-Type: image/gif\r\n\r\n' % ('sample')bs = bs + header.encode('utf8')bs = bs + filebytestailer = '\r\n--%s--\r\n' % (boundary)bs = bs + tailer.encode('ascii')headers = {'Content-Type': 'multipart/form-data; boundary=%s' % boundary,'Connection': 'Keep-Alive','Expect': '100-continue',}response = requests.post(url, params='', data=bs, headers=headers)return response.json()if __name__ == '__main__':# 測試ret = getCode('../captcha_imgs/1544505888345_3.png')print(ret)3、常見的驗證碼的種類
3.1 url地址不變,驗證碼不變
這是驗證碼里面非常簡單的一種類型,對應的只需要獲取驗證碼的地址,然后請求,通過打碼平臺識別即可。
3.2 url地址不變,驗證碼變化
這種驗證碼的類型是更加常見的一種類型,對于這種驗證碼,大家需要思考:
??在登錄的過程中,假設我輸入的驗證碼是對的,對方服務器是如何判斷當前我輸入的驗證碼是顯示在我屏幕上的驗證碼,而不是其他的驗證碼呢?
??在獲取網頁的時候,請求驗證碼,以及提交驗證碼的時候,對方服務器肯定通過了某種手段驗證我之前獲取的驗證碼和最后提交的驗證碼是同一個驗證碼,那這個手段是什么手段呢?
很明顯,就是通過cookie來實現的,所以對應的,在請求頁面,請求驗證碼,提交驗證碼的到時候需要保證cookie的一致性,對此可以使用requests.session來解決
總結
以上是生活随笔為你收集整理的十七、打码平台的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 四人小组项目(对项目进行的修改与重写)
- 下一篇: 爬虫——动作链、xpath、打码平台使用