用9kw网站搞定验证码

https://www.9kw.eu/是一个互帮互助的人工识别验证码平台,你需要注册,完成一定的验证码识别后会获得自己的API_KEY。觉得无聊就刷刷分,帮别人,到你用时别人也会帮你。这里贴一些代码,只用将自己的API_KEY填写并将x.PNG更换为想要识别的图片就行。

import time
import re
import urllib2
import urllib
from PIL import Image
from io import BytesIO


class CaptchaAPI:
    def __init__(self, timeout=60):
        self.api_key = '××××××××'
        self.timeout = timeout
        self.url = 'https://www.9kw.eu/index.cgi'

    def solve(self, img):
        img_buffer = BytesIO()
        img.save(img_buffer, format="PNG")
        img_data = img_buffer.getvalue()
        captcha_id = self.send(img_data)
        start_time = time.time()
        while time.time() < start_time + self.timeout:
            try:
                text = self.get(captcha_id)
            except CaptchaError:
                pass  # CAPTCHA still not ready
            else:
                if text != 'NO DATA':
                    if text == 'ERROR NO USER':
                        raise CaptchaError('Error: no user available to solve CAPTCHA')
                    else:
                        print 'CAPTCHA solved!'
                        return text
            print 'Waiting for CAPTCHA ...'
        raise CaptchaError('Error: API timeout')

    def send(self, img_data):
        print 'submitting...'
        data = {
            'action': 'usercaptchaupload',
            'apikey': self.api_key,
            'file_upload_01': img_data.encode('base64'),
            'base64': '1',
            'selfsolve': '0',
            'maxtimeout': str(self.timeout)
        }
        encoded_data = urllib.urlencode(data)
        request = urllib2.Request(self.url, encoded_data)
        response = urllib2.urlopen(request)
        result = response.read()
        self.check(result)
        return result

    def get(self, captcha_id):
        data = {
            'action': 'usercaptchacorrectdata',
            'id': captcha_id,
            'apikey': self.api_key,
            'info': '1'
        }
        encoded_data = urllib.urlencode(data)
        response = urllib2.urlopen(self.url + '?' + encoded_data)
        result = response.read()
        self.check(result)
        return result

    def check(self, result):
        if re.match('00\d\d \w+', result):
            raise CaptchaError('API error:' + result)


class CaptchaError(Exception):
    pass

if __name__ == '__main__':
    captcha = CaptchaAPI()
    img = Image.open('x.PNG')
    text = captcha.solve(img)
    print text
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值