爬虫日记-验证码识别

Posted chanyuli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬虫日记-验证码识别相关的知识,希望对你有一定的参考价值。

识别验证码

这个例子,是我们用python代码通过采用第三方来进行识别验证码。

我们用的是云打码这个网站。先在这个网站上把该注册的该申请的东西全都弄好。

然后把他的PythonHTTP调用示例下载来。

代码就在里面,具体怎么实现的我们就不用去理解了,要是能理解了,那可得多屌。

接下来我们只用去我们想要识别验证码的网站上把这个图片的连接爬取下来就好了,

再对这个连接进行requests请求获取图片的二进制数据

然后把图片保存下来,最后把这个图片文件当做参数传进已经下载来的代码里面就好了。

main.py

import  requests
from lxml import etree
from 爬虫课程.破解验证码.code import YDMHttp

def shibie(imagefile,codetype):
    username = 'chanyuli'

    # 密码
    password = 'qw171222338'

    # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
    appid = 8956

    # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
    appkey = '3e05b8c8571c3cc219160a3f449501db'

    # 图片文件
    filename = imagefile

    # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html
    codetype = codetype

    # 超时时间,秒
    timeout = 15

    # 检查
    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))

url='https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx'

headers ='User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'

page_text=requests.get(url=url,headers=headers).text

tree=etree.HTML(page_text)

src=tree.xpath('//*[@id="imgCode"]/@src')[0]

src_url='https://so.gushiwen.org'+src

img_data=requests.get(url=src_url,headers=headers).content

# save_path=r'D:\python\untitled\爬虫课程\国家药品监管局'+src+'.jpg'
with open('./code.jpg','wb')as fw:
    fw.write(img_data)


text=shibie('code.jpg',1004)
print(text)

code.py

import http.client, mimetypes, urllib, json, time, requests


######################################################################

class YDMHttp:
    apiurl = 'http://api.yundama.com/api.php'
    username = ''
    password = ''
    appid = ''
    appkey = ''

    def __init__(self, username, password, appid, appkey):
        self.username = username
        self.password = password
        self.appid = str(appid)
        self.appkey = appkey

    def request(self, fields, files=[]):
        response = self.post_url(self.apiurl, fields, files)
        response = json.loads(response)
        return response

    def balance(self):
        data = 'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['balance']
        else:
            return -9001

    def login(self):
        data = 'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['uid']
        else:
            return -9001

    def upload(self, filename, codetype, timeout):
        data = 'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)
        file = 'file': filename
        response = self.request(data, file)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['cid']
        else:
            return -9001

    def result(self, cid):
        data = 'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'cid': str(cid)
        response = self.request(data)
        return response and response['text'] or ''

    def decode(self, filename, codetype, timeout):
        cid = self.upload(filename, codetype, timeout)
        if (cid > 0):
            for i in range(0, timeout):
                result = self.result(cid)
                if (result != ''):
                    return cid, result
                else:
                    time.sleep(1)
            return -3003, ''
        else:
            return cid, ''

    def report(self, cid):
        data = 'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid,
                'appkey': self.appkey, 'cid': str(cid), 'flag': '0'
        response = self.request(data)
        if (response):
            return response['ret']
        else:
            return -9001

    def post_url(self, url, fields, files=[]):
        for key in files:
            files[key] = open(files[key], 'rb');
        res = requests.post(url, files=files, data=fields)
        return res.text

但是,这个网站验证码识别不是百分百正确的,我试了好几次都有错误的。

以上是关于爬虫日记-验证码识别的主要内容,如果未能解决你的问题,请参考以下文章

第二十三节:scrapy爬虫识别验证码图片验证码识别

中文项目:快速识别验证码,CNN也能为爬虫保驾护航

[爬虫]微博宫格验证码识别(3.1)

爬虫验证码识别方法

Python3网络爬虫实战-43极验滑动验证码的识别

如何python爬虫识别验证码