上接: 第三章:数据解析(xpath法)
下接: 第五章:模拟登录人人网
第四章:验证码
是网页制定的一种反爬机制,需要识别验证图片中的数据,用于模拟登录。
识别验证码的操作:
4.1超级鹰平台的操作
首先就是登录/注册操作:
进入用户中心之后确认自己的题分>10分,没有的话就充钱(1元=1000分)
题分准备之后,进入软件ID——生成一个软件ID——此时生成的软件ID的数字之后有用:
然后进入开发文档,将相应的python代码下载下来
在本地打开
下载好的python模板中,需要将下面这个代码的数据进行修改,分别填写:你的用户名/密码/软件ID:
chaojiying = Chaojiying_Client('超级鹰用户名', '超级鹰用户名的密码', '96001')
然后运行代码:
#!/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__':
chaojiying = Chaojiying_Client('your account', 'password', 'softwareID') #用户中心>>软件ID 生成一个替换 96001
im = open('a.jpg', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
print(chaojiying.PostPic(im, 1902)) #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()
就完成了借助这个超级鹰的平台帮我们识别验证码的功能:
对于模板代码中这句话中的“1902”指的是验证码类型,相应类型对应的编号上官网“价格体系查询”
print(chaojiying.PostPic(im, 1902))
4.2古诗词网验证码识别
使用打码平台识别验证码的编码流程:
- 将验证码图片进行本地下载
- 调用平台提供的示例代码进行图片数据识别
找到验证码图片对应的url:
而scr中的url以/开头,说明地址不全,将验证码图片在新窗口打开:
代码实战:
#!/usr/bin/env python
# coding:utf-8
import requests
import json
from lxml import etree
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()
#UA伪装
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.11 Safari/537.36'
}
url='https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
#将验证图片下载到本地,找到img对应的src值
page_text=requests.get(url=url,headers=headers).text
tree=etree.HTML(page_text)
code_img='https://so.gushiwen.cn/'+tree.xpath('//*[@id="imgCode"]/@src')[0]
img_data=requests.get(url=code_img,headers=headers).content
with open('D://code.jpg','wb') as fp:
fp.write(img_data)
chaojiying = Chaojiying_Client('your account', 'password', 'softwareID') # 用户中心>>软件ID 生成一个替换 96001
im = open('D://code.jpg', 'rb').read() # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
codeType=1004
print(chaojiying.PostPic(im,codeType))
输出结果:
{
'err_no': 0, 'err_str': 'OK', 'pic_id': '8133316155708000006', 'pic_str': 'CVT2', 'md5': '3dc3148111c030a3e8bc14fcf4534039'}
进程已结束,退出代码0
如图所示: