验证码
验证码
某些网站访问需要验证码,并且在访问过于频繁的时候也会出现验证码。这时候验证码识别就出现了。
验证码识别
- 人工识别
- 打码平台 ,付费(人工+机器学习)
- 机器学习
下面以云打码平台的使用为例
云打码平台的使用
- 对携带验证码的页面数据进行抓取
- 可以将页面数据中验证码进行解析,验证码图片下载到本地
- 可以将验证码图片提交给三方平台进行识别,返回验证码图片上的数据值
- 云打码平台:http://www.yundama.com/demo.html
- 在官网中进行注册(普通用户和开发者用户)
- 使用普通账户充值
- 登录开发者用户:
- 实例代码的下载(开发文档-》调用实例及最新的DLL-》PythonHTTP实例下载)
- 创建一个软件:我的软件-》添加新的软件
- 使用示例代码中的源码文件中的代码进行修改,让其识别验证码图片中的数据值
- 在官网中进行注册(普通用户和开发者用户)
- 云打码平台:http://www.yundama.com/demo.html
人人网登录示例
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import http.client, mimetypes, urllib, json, time, requests
from lxml import etree
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 get_code(filename, codetype):
"""
:param filename: 文件路径
:param codetype: 打码类型,例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。
在此查询所有类型 http://www.yundama.com/price.html
:return:
"""
# 用户名
USERNAEM = 'xxx'
# 密码
PASSWORD = 'xxx'
# 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
appid = 8411
# 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
appkey = '584a13ed22a2a14a391296c409493ed4'
# 超时时间,秒
timeout = 60
# 检查
if (USERNAEM == 'username'):
print('请设置好相关参数再测试')
else:
# 初始化
yundama = YDMHttp(USERNAEM, 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 cid, result
FILENAME = 'tmp.jpg'
# get_code(filename, 1000)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
}
code_XP = '//*[@id="verifyPic_login"]/@src'
start_url = 'http://www.renren.com/SysHome.do'
session = requests.Session()
res1 = session.get(start_url, headers=headers)
tree = etree.HTML(res1.text)
icode_url = tree.xpath(code_XP)
if icode_url:
icode_url = icode_url[0]
with open(FILENAME, 'wb') as f:
f.write(session.get(icode_url, headers=headers).content)
code = get_code(FILENAME, 1000)[1]
else:
code = ''
url2 = 'http://login.renren.com/ajax/getEncryptKey'
res2 = session.get(url2, headers=headers)
# print(res2.json())
rkey = res2.json().get('rkey')
url3 = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=201964034544 '
data = {
"email": "username",
"icode": code,
"origURL": "http://www.renren.com/home",
"domain": "renren.com",
"key_id": "1",
"captcha_type": "web_login",
"password": "password", # 通过抓包获取加密后的密码
"rkey": rkey,
"f": "",
}
res3 = session.post(url3, headers=headers, data=data)
detail_url = 'http://www.renren.com/289676607/profile' # 个人页面
page_text = session.get(url=detail_url, headers=headers).text
with open('./renren.html', 'w', encoding='utf-8') as fp:
fp.write(page_text)