爬虫之验证码识别

验证码识别
反爬机制:
验证码.识别验证码图片中的数据,用于模拟登陆操作。
识别验证码的操作:
人工肉眼识别。(不推荐)
第三方自动识别(推荐)
超级鹰: https://www.chaojiying.com/
超级鹰的使用流程:
注册用户
登录:查询该用户是否还有剩余的题分
创建一个软件:用户中心 -> 软件ID -> 新建一个软件 -> 提交(软件id和秘钥)
下载示例代码:开发文档-> Python示例下载
实战:识别古诗文网登录页面中的验证码
使用打码平台识别验证码的编码流程:
  将验证码图片进行本地下载
  调用平台提供的示例代码进行图片数据识别
 1 import requests
 2 from hashlib import md5
 3 
 4 class Chaojiying_Client(object):
 5 
 6     def __init__(self, username, password, soft_id):
 7         self.username = username
 8         password =  password.encode('utf8')
 9         self.password = md5(password).hexdigest()
10         self.soft_id = soft_id
11         self.base_params = {
12             'user': self.username,
13             'pass2': self.password,
14             'softid': self.soft_id,
15         }
16         self.headers = {
17             'Connection': 'Keep-Alive',
18             'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
19         }
20 
21     def PostPic(self, im, codetype):
22         """
23         im: 图片字节
24         codetype: 题目类型 参考 http://www.chaojiying.com/price.html
25         """
26         params = {
27             'codetype': codetype,
28         }
29         params.update(self.base_params)
30         files = {'userfile': ('ccc.jpg', im)}
31         r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
32         return r.json()
33 
34     def ReportError(self, im_id):
35         """
36         im_id:报错题目的图片ID
37         """
38         params = {
39             'id': im_id,
40         }
41         params.update(self.base_params)
42         r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
43         return r.json()
CodeClass

 1 # 验证码识别
 2 # 古诗文网
 3 import requests
 4 from lxml import etree
 5 from CodeClass import Chaojiying_Client
 6 # 封装一个验证码识别的函数
 7 def get_code(filename,type):
 8     chaojiying = Chaojiying_Client("username", "password", '121212')  # 用户中心>>软件ID 生成一个替换 96001
 9     im = open(filename, 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
10     return chaojiying.PostPic(im, type)["pic_str"]
11 
12 if __name__ == '__main__':
13     url = "https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx"
14     headers = {
15         "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
16     }
17     page_text = requests.get(url=url,headers=headers).text
18 
19     # 使用etree来解析网页数据
20     tree = etree.HTML(page_text)
21     img_src = 'https://so.gushiwen.cn' + tree.xpath("//img[@id='imgCode']/@src")[0]
22     img_content = requests.get(url=img_src,headers=headers).content
23 
24     # 将验证码保存到了本地
25     with open("./古诗文网验证码图片.jpg","wb") as f:
26         f.write(img_content)
27     print("验证码下载成功!")
28 
29     #使用超级鹰的验证码识别功能来进行验证码的识别
30     code_text = get_code('古诗文网验证码图片.jpg',1902)
31     print("识别的验证码为:", code_text)
古诗文网验证码识别

 

 
posted @ 2020-08-26 20:19  My帝王源  阅读(91)  评论(0)    收藏  举报