雲打碼實現處理驗證碼
處理驗證碼,我們需要借助第三方平台來幫我們處理,個人認為雲打碼處理驗證碼的准確度還是可以的
首先第一步,我們得先注冊一個雲打碼的賬號,普通用戶和開發者用戶都需要注冊一下
然后登陸普通用戶,登陸之后的界面是這樣的,

你需要有幾分才可以使用它.
第二步登陸開發者用戶:

然后點擊開發文檔

進入之后點擊下載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)
