6、Python Requests库高级操作【2】
概要:
- 验证码识别
- 模拟登录
1、验证码识别
案例需求:
-
将古诗文网中的验证码进行识别
-
需要基于一些线上的打码平台进行验证码识别
- 需要将页面中的验证码图片进行下载,然后将其提交给平台进行识别
-
常用的打码平台:
-
超级鹰:
-
云打码
-
打码兔、
等等等。。。
-
import random
from lxml import etree
import requests
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36',
}
url = 'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx'
page_text = requests.get(url=url,headers=headers).text
tree = etree.HTML(page_text)
img_src = 'https://so.gushiwen.org'+tree.xpath('//*[@id="imgCode"]/@src')[0]
img_data = requests.get(url=img_src,headers=headers).content
with open('./code.jpg','wb') as fp:
fp.write(img_data)
使用超级鹰进行验证码识别
官网:https://www.chaojiying.com/
使用流程
- 注册:注意用户中心的账号
- 登录:登录用户中心身份的账号
- 题分充值
- 创建一个软件ID:905940
- 下载平台提供的示例代码
完整代码
#!/usr/bin/env python
# coding:utf-8
import requests
from hashlib import md5
class Chaojiying_Client(object):
def __init__(self, username, password, soft_id):
self.username = username
password = password.encode('utf8')
self.password = md5(password).hexdigest()
self.soft_id = soft_id
self.base_params = {
'user': self.username,
'pass2': self.password,
'softid': self.soft_id,
}
self.headers = {
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
}
def PostPic(self, im, codetype):
"""
im: 图片字节
codetype: 题目类型 参考 http://www.chaojiying.com/price.html
"""
params = {
'codetype': codetype,
}
params.update(self.base_params)
files = {'userfile': ('ccc.jpg', im)}
r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
return r.json()
def ReportError(self, im_id):
"""
im_id:报错题目的图片ID
"""
params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
return r.json()
if __name__ == '__main__':
def transform_codeImg(imgPath,imgType):
chaojiying = Chaojiying_Client('xjs2020', 'xxxxx', '905940') #用户中心>>软件ID 生成一个替换 96001
im = open(imgPath, 'rb').read()#本地图片文件路径 来替换 a.jpg 有时WIN系统须要
#return chaojiying.PostPic(im, imgType)['pic_str']#1902 验证码类型
print(chaojiying.PostPic(im, imgType)['pic_str'])
transform_codeImg('./code.jpg',1004)
>>>llvm
2、模拟登录
模拟登录古诗文网
- 动态变化的请求参数,如何处理?
- 两种处理方式
- 有可能会隐藏在前台页面源码中
- 有可能是存在于某一个js函数代码
- 基于抓包工具做全局搜索
- 两种处理方式
完整代码
from lxml import etree
import requests
from hashlib import md5
class Chaojiying_Client(object):
def __init__(self, username, password, soft_id):
self.username = username
password = password.encode('utf8')
self.password = md5(password).hexdigest()
self.soft_id = soft_id
self.base_params = {
'user': self.username,
'pass2': self.password,
'softid': self.soft_id,
}
self.headers = {
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
}
def PostPic(self, im, codetype):
"""
im: 图片字节
codetype: 题目类型 参考 http://www.chaojiying.com/price.html
"""
params = {
'codetype': codetype,
}
params.update(self.base_params)
files = {'userfile': ('ccc.jpg', im)}
r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
return r.json()
def ReportError(self, im_id):
"""
im_id:报错题目的图片ID
"""
params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
return r.json()
if __name__ == '__main__':
def transform_codeImg(imgPath,imgType):
chaojiying = Chaojiying_Client('xjs2020', 'xxxx', '905940') #用户中心>>软件ID 生成一个替换 96001
im = open(imgPath, 'rb').read()#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
return chaojiying.PostPic(im, imgType)['pic_str']#1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()
session = requests.Session()
url = 'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx'
page_text = session.get(url,headers=headers).text
tree = etree.HTML(page_text)
img_src = 'https://so.gushiwen.org'+tree.xpath('//*[@id="imgCode"]/@src')[0]
img_data = session.get(url=img_src,headers=headers).content
__VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0]
__VIEWSTATEGENERATOR = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0]
with open('./code.jpg','wb') as fp:
fp.write(img_data)
code_text = transform_codeImg('./code.jpg',1004)#验证码内容
print(code_text)
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': '15801500903',
'pwd': 'xxxxxx',
'code': code_text,
'denglu': '登录',
}
page_text = session.post(url=login_url,headers=headers,data=data).text
with open('./login.html','w',encoding='utf-8') as fp:
fp.write(page_text)
登录成功效果图