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