调用第三方打码平台破解图片验证码
云打码平台处理验证码的实现流程:
- 对携带验证码的页面数据进行抓取
- 可以将页面数据中验证码进行解析,验证码图片下载到本地
- 可以将验证码图片提交给三方平台进行识别,返回验证码图片上的数据值
# 云打码平台api
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
def getCode(codeImg):
# 用户名
username = 'wualin'
# 密码
password = '09212427'
# 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
appid = 6524
# 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
appkey = '4f63d68488c9f0b1f090f7ccbe79e399'
# 图片文件
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
调用云打码平台api破解验证码进行豆瓣电影登陆
import discern_code
# 获取验证码图片保存到本地
import requests
from lxml import etree
import re
url = 'https://www.douban.com/accounts/login?source=movie'
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}
page_text = requests.get(url=url,headers=headers).text
# 使用xpath解析出验证码图片的url
tree = etree.HTML(page_text)
codeImg_url = tree.xpath('//*[@id="captcha_image"]/@src')[0]
# 获取到图片的二进制数据保存到本地
code_img = requests.get(url=codeImg_url,headers=headers)
with open('code.png','wb') as f:
f.write(code_img.content)
code = discern_code.getCode('code.png')
# 获取captcha-id的值
c_id = re.findall('<img id="captcha_image" .*?id=(.*?)&.*?',page_text)[0]
login_url = 'https://accounts.douban.com/login'
data = {
'source': 'movie',
'redir': 'https://movie.douban.com/',
'form_email': '1032298871@qq.com',
'form_password': '09212427zlh',
'captcha-solution': code,
'captcha-id': c_id,
'login': '登录',
}
response = requests.post(url=login_url,data=data,headers=headers)
with open('豆瓣.html','w',encoding='utf-8') as f:
f.write(response.text)