使用云打码平台识别验证码
相关的门户网站在进行登录的时候,如果用户连续登录的次数超过3次或者5次的时候,就会在登录页中动态生成验证码。通过验证码达到分流和反爬的效果。
云打码平台处理验证码的实现流程:
云打码平台处理验证码的实现流程:
- 1.对携带验证码的页面数据进行抓取 - 2.可以将页面数据中验证码进行解析,验证码图片下载到本地 - 3.可以将验证码图片提交给三方平台进行识别,返回验证码图片上的数据值 - 云打码平台: - 1.在官网中进行注册(普通用户和开发者用户) - 2.登录开发者用户: - 1.实例代码的下载(开发文档-》调用实例及最新的DLL-》PythonHTTP实例下载) - 2.创建一个软件:我的软件-》添加新的软件 -3.使用示例代码中的源码文件中的代码进行修改,让其识别验证码图片中的数据值
代码展示:
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 import requests from lxml import etree from urllib import request # 封装识别验证码图片的函数 def getCodeText(codeType,filePath): result = None # 普通用户名 username = 'jeremy0820' # 密码 password = '0820_ab' # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得! appid = 6003 # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得! appkey = '1f4b564483ae5c907a1d34f8e2f2776c' # 图片文件 filename = filePath # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html codetype = codeType # 超时时间,秒 timeout = 30 # 检查 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
模拟登陆
url = 'http://www.renren.com/' headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36' } page_text = requests.get(url=url,headers=headers,proxies={'http':'121.8.98.196:80'}).text # 解析出验证码图片的地址 tree = etree.HTML(page_text) code_img_url = tree.xpath('//*[@id="verifyPic_login"]/@src')[0] # 保存图片 request.urlretrieve(url=code_img_url,filename='./code.jpg') # 使用打码平台识别验证码 code_text = getCodeText(2004,'./code.jpg') print(code_text) #模拟登陆 #抓不到就是时间戳超时 login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019402136904' data = { "email": "www.zhangbowudi@qq.com", "icode": code_text, "origURL": "http://www.renren.com/home", "domain": "renren.com", "key_id": "1", "captcha_type": "web_login", "password": "b5b7cc084ec2c8b2fa9ec88ebb55dddb07ce2809f14e98db78ddcfa7159b8ae2", "rkey": "449e2cdaaefe6364b26d5b62baab86f5", "f": "http%3A%2F%2Fwww.renren.com%2F970683046", } #创建一个会话对象 session = requests.Session() # 产生cookie response = requests.post(url=login_url,headers=headers,data=data,proxies={'http':'121.8.98.196:80'}) # print(response.status_code) # page_text = response.text # print(page_text) # 该次请求发送必须携带cookie detail_url = 'http://www.renren.com/970683046/profile' requests.get(url=detail_url,headers=headers).text
cookie的应用和处理
- cookie:服务器端记录客户端的相关状态
- 处理cookie的方式:
- 手动处理:不建议
- 自动处理:回话对象Session,该对象可以像requests模块一样进行网络请求的发送(get,post)。session进行的请求发送可以自动携带和处理cookie。
#基于cookie的案例分析:https://xueqiu.com/ #1.从首页中获取详情页的url #发现:首页中的新闻数据是动态加载出来(ajax) json数据中taget对应的value值就是详情页的url import requests headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36' } #自动获取cookie,cookie就会自动存储到session中 session = requests.Session() session.get('https://xueqiu.com/',headers=headers) #捕获ajax数据包获取的url url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=-1&count=10&category=-1' #携带cookie进行的请求发送 dic_json = session.get(url=url,headers=headers).json() print(dic_json) #从响应数据中获取详情页的url # for dic in dic_json['list']: # # print(dic) # d = dic['data'] # detail_url = 'https://xueqiu.com'+d['target'] # print(detail_url)