码迷,mamicode.com
首页 > 其他好文 > 详细

爬虫日记-验证码识别

时间:2019-09-17 22:31:38      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:flag   测试   print   appid   类型   field   from   word   jpg   

识别验证码

这个例子,是我们用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

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

爬虫日记-验证码识别

标签:flag   测试   print   appid   类型   field   from   word   jpg   

原文地址:https://www.cnblogs.com/chanyuli/p/11537431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!