美文网首页
爬虫---验证码

爬虫---验证码

作者: riverstation | 来源:发表于2018-07-19 19:35 被阅读54次

自动识别验证码

(1)光学识别 OCR 其实就是一个软件

  • 别对他期望太高,识别率80% 90%
    *训练它
    代码识别

pip install pytesseract
pip install pillow

转化为灰度图片

img = img.convert('L')
img.show()

二值化处理

threshold = 140
table = []
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)
out = img.point(table, '1')
out.show()

img = img.convert('RGB')
enhancer = ImageEnhance.Color(img)
enhancer = enhancer.enhance(0)
enhancer = ImageEnhance.Brightness(enhancer)
enhancer = enhancer.enhance(2)
enhancer = ImageEnhance.Contrast(enhancer)
enhancer = enhancer.enhance(8)
enhancer = ImageEnhance.Sharpness(enhancer)
img = enhancer.enhance(20)

实例代码如下

import pytesseract
from PIL import Image
from PIL import ImageEnhance

def shibie(imagepath):
    img = Image.open(imagepath)

    img = img.convert('RGB')
    enhancer = ImageEnhance.Color(img)
    enhancer = enhancer.enhance(0)
    enhancer = ImageEnhance.Brightness(enhancer)
    enhancer = enhancer.enhance(2)
    enhancer = ImageEnhance.Contrast(enhancer)
    enhancer = enhancer.enhance(8)
    enhancer = ImageEnhance.Sharpness(enhancer)
    img = enhancer.enhance(20)

    # 转化为灰度图片
    img = img.convert('L')
    # img.show()

    # 二值化处理
    threshold = 140
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    out = img.point(table, '1')
    # out.show()

    img = img.convert('RGB')

    return pytesseract.image_to_string(img)
shibie('code.png')

打码平台,专业,识别率高,花钱

云打码平台使用

用户注册(充钱)、开发者注册(创建应用,使用应用识别验证码服务)

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

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

# 用户名
# 普通账号的用户名
username    = ''

# 密码
# 普通账号的密码
password    = ''                            

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

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

# 图片文件
filename    = './png/e6ay.png'                        

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

# 超时时间,秒
timeout     = 60                                    

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

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

相关文章

  • 三. 突破反爬虫

    1.反爬虫措施一般分为四类:①基于验证码的反爬虫:传统验证码、逻辑验证码、滑动验证码、google访问时弹出的验证...

  • 最头疼的验证码,爬虫中的验证码如何破?

    "写爬虫的时候遇到过反爬虫措施中的验证码吗,最终是怎样解决的",面试官经常这么问. 什么是验证码?...

  • 58手势验证码的分析

    做爬虫的小伙伴们肯定都深有体会,爬虫要是遇到验证码了基本上就是GG了。于是爬虫工作者和验证码之间必有一战。随着w...

  • 爬虫笔记(10)插曲 挑战极限验证码

    1.前言 既然有爬虫的存在那就有反爬虫技术的存在,验证码是常见手段,不过最近发现不少网站使用极限验证码。对于普通验...

  • 使用MaskRCNN进行手势验证码识别

    相信做某本地生活平台网站爬虫的同学,饱受手势验证码的困扰,这类验证码相比滑块验证码,识别难度更大。 看到这种验证码...

  • Python 实现识别弱图片验证码

    目前,很多网站为了防止爬虫肆意模拟浏览器登录,采用增加验证码的方式来拦截爬虫。验证码的形式有多种,最常见的就是图片...

  • 验证码的识别

    验证码是一种反爬虫的措施,目前的验证码主要有图像验证码、极验滑动验证码、点触验证码、微博宫格验证码等。根据不同类型...

  • 爬虫遇到验证码拦路,该怎么破解?

    我们知道,在爬虫采集数据的过程中一旦出现验证码,正在进行的数据采集工作就会因此中断。验证码作为区分人类与爬虫的反爬...

  • 18《Python 原生爬虫教程》使用 Pytesseract

    在爬虫开发中我们经常会遇到一种反爬虫的手段就是验证码,那么如何才能绕过验证码拿到我们想要的数据呢?这节课我给大家介...

  • 爬虫

    爬虫 按照一定规则自动抓取网络信息的程序 反爬虫 user-agent, referer,验证码 单位时间访问次数...

网友评论

      本文标题:爬虫---验证码

      本文链接:https://www.haomeiwen.com/subject/wmcxmftx.html