python3爬虫验证码识别——超级鹰打码平台的使用&实战:识别古诗文网登录页面中的验证码

一、验证码和爬虫之间的爱恨情仇?

反爬机制:验证码.识别验证码图片中的数据,用于模拟登陆操作。

二、识别验证码的操作:

  1. 人工肉眼识别。(不推荐)
  2. 第三方自动识别(推荐)
    - 超级鹰打码:https://www.chaojiying.com/

超级鹰打码平台的使用流程:
1. 注册:
2. 登录:

  • 查询该用户是否还有剩余的题分
    在这里插入图片描述

  • 创建一个软件:超级鹰首页>用户中心>软件ID>生成一个软件ID>录入软件名称>提交(软件id和秘钥)
    在这里插入图片描述
    在这里插入图片描述

  • 下载示例代码:开发文档>点此下载python示例
    在这里插入图片描述
    在这里插入图片描述
    示例代码如下:

#!/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('超级鹰用户名', '超级鹰用户名的密码', '96001')	#用户中心>>软件ID 生成一个替换 96001
	im = open('a.jpg', 'rb').read()													#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
	print chaojiying.PostPic(im, 1902)												#1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()


只需要把最后一行的print后面加括号()即可

最后三行所需要的参数:

超级鹰用户名和密码:注册这个网站的

软件ID:在用户中心处获取
在这里插入图片描述
图片路径:注意相对路径和绝对路径,案例中附带了一个图片a.jpg

验证码类型:在价格体系中查询 http://www.chaojiying.com/price.html

运行结果(通过程序可知是json格式的数据):
在这里插入图片描述
返回说明:
err_no,(数值) 返回代码
err_str,(字符串) 中文描述的返回信息
pic_id,(字符串) 图片标识号,或图片id号
pic_str,(字符串) 识别出的结果
md5,(字符串) md5校验值,用来校验此条数据返回是否真实有效

三、实战:识别古诗文网登录页面中的验证码。

使用打码平台识别验证码的编码流程:
- 将验证码图片进行本地下载
- 调用平台提供的示例代码进行图片数据识别

在这里插入图片描述

import requests
from lxml import etree
from hashlib import md5

# 封装识别验证码图片的函数
def getCodeText(userName, password, appId, imgUrl):
    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(userName, password, appId)  # 用户中心>>软件ID 生成一个替换 96001
        im = open(imgUrl, 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
        # print(chaojiying.PostPic(im, 1902))  # 1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
    return chaojiying.PostPic(im, 1902)

# 将验证码图片下载到本地
url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
headers = {
    
    
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
    }
page_text = requests.get(url=url,headers=headers).text

# 解析验证码图片img中src属性值
tree = etree.HTML(page_text)
img_url = tree.xpath('//img[@id="imgCode"]/@src')[0]
img_url = 'https://so.gushiwen.cn/' + img_url
# print(img_url)
img_data = requests.get(url=img_url,headers=headers).content
# 将验证码图片存到本地
with open('./code.jpg','wb') as fp:
    fp.write(img_data)


# 调用打码平台的示例程序进行验证码图片数据识别
result = getCodeText('你的超级鹰用户名','你的超级鹰密码', '你的超级鹰appid', '需要识别的验证码图片在本地的路径')
print(result)


在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44827418/article/details/113936112