验证码处理
云打码平台的使用
云打码平台处理验证码的实现流程:- 官网url:http://www.yundama.com/demo.html
- 1.对携带验证码的页面数据进行抓取 - 2.可以将页面数据中验证码进行解析,验证码图片下载到本地 - 3.可以将验证码图片提交给三方平台进行识别,返回验证码图片上的数据值 - 云打码平台: - 1.在官网中进行注册(普通用户和开发者用户) - 2.登录开发者用户: - 1.实例代码的下载(开发文档-》调用实例及最新的DLL-》PythonHTTP实例下载) - 2.创建一个软件:我的软件-》添加新的软件 -3.使用示例代码中的源码文件中的代码进行修改,让其识别验证码图片中的数据值
案例:
爬取人人网登录后的个人主页
#将验证码识别的操作封装成一个函数 def getCodeText(userName,passWord,imgPath,codeType): result = None # 普通用户用户名 username = userName # 密码 password = passWord # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得! appid = 7166 # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得! appkey = '18e36137d7308a1c6218de7f148d95ee' # 图片文件 filename = imgPath # 验证码类型,# 例: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
import requests import json import time from lxml import etree from urllib import request url = 'http://www.renren.com/' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.12 Safari/537.36' } page_text = requests.get(url=url,headers=headers).text #识别人人网中的验证码图片 tree = etree.HTML(page_text) code_img_url = tree.xpath('//*[@id="verifyPic_login"]/@src')[0] request.urlretrieve(code_img_url,'./code.jpg') #解析验证码图片 code_text = getCodeText('rest_zwq','xx密码xx','./code.jpg',2004) #登录 login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=201924831467' 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":"436e61246c5c1c36a288c0e625b4e10f9b9c8df72b7a3f6e06a182c38d2286cb", "rkey":"77a46f4376c93a80d35930b2cacb6a8d", "f":"", } # 创建一个session对象 session = requests.Session() #使用session进行请求的发送:获取cookie,且将cookie保存到session中 session.post(login_url,data=data,headers=headers) #获取个人主页对应的页面数据 detail_url = 'http://www.renren.com/289676607/profile' #该次请求发送是就已经携带了cookie page_text = session.get(url=detail_url,headers=headers).text with open('./renren.html','w',encoding='utf-8') as fp: fp.write(page_text)
爬取古诗文网数据
需要携带类似于token的动态数据
#对古诗文网进行模拟登录 from lxml import etree from urllib import request headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.12 Safari/537.36' } session = requests.Session() #获取验证码图片 url = 'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx' page_text = requests.get(url,headers=headers).text tree = etree.HTML(page_text) code_img_url = 'https://so.gushiwen.org'+tree.xpath('//*[@id="imgCode"]/@src')[0] img_data = session.get(url=code_img_url,headers=headers).content with open('./gushiwen.jpg','wb') as fp: fp.write(img_data) # 拿到验证码 code_text = getCodeText('rest_zwq','zwq1156145880','./gushiwen.jpg',1004) # 执行登录 # 获取类似token的动态数据 __VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0] __VIEWSTATEGENERATOR = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0] login_url = 'https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx' data = { "__VIEWSTATE":__VIEWSTATE, "__VIEWSTATEGENERATOR":__VIEWSTATEGENERATOR, "from":"http://so.gushiwen.org/user/collect.aspx", "email":"www.zhangbowudi@qq.com", "pwd":"bobo328410948", "code":code_text, "denglu":"登录", } page_text = session.post(url=login_url,data=data,headers=headers).text with open('./gushiwen.html','w',encoding='utf-8') as fp: fp.write(page_text)