AttributeError:_退出_,同时在python 2.7上缓冲下载的图像
我正在尝试用python脚本自动识别验证码文件。 然而,经过几天的努力,我的功能似乎远远没有达到预期的效果 此外,我得到的回溯信息不足以帮助我进一步调查 以下是我的功能:AttributeError:_退出_,同时在python 2.7上缓冲下载的图像,python,image,python-2.7,python-imaging-library,buffering,Python,Image,Python 2.7,Python Imaging Library,Buffering,我正在尝试用python脚本自动识别验证码文件。 然而,经过几天的努力,我的功能似乎远远没有达到预期的效果 此外,我得到的回溯信息不足以帮助我进一步调查 以下是我的功能: def getmsg(): solved = '' probe_request = [] try: probe_request = api.messages.get(offset = 0, count = 2) except apierror, e: if e.c
def getmsg():
solved = ''
probe_request = []
try:
probe_request = api.messages.get(offset = 0, count = 2)
except apierror, e:
if e.code is 14:
key = e.captcha_sid
imagefile = cStringIO.StringIO(urllib.urlopen(e.captcha_img).read())
img = Image.open(imagefile)
imagebuf = img.load()
with imagebuf as captcha_file:
captcha = cptapi.solve(captcha_file)
finally:
while solved is None:
solved = captcha.try_get_result()
tm.sleep(1.500)
print probe_request
以下是回溯:
Traceback (most recent call last):
File "myscript.py", line 158, in <module>
getmsg()
File "myscript.py", line 147, in getmsg
with imagebuf as captcha_file:
AttributeError: __exit__
这导致:
captcha = cptapi.solve(captcha_file)
File "/usr/local/lib/python2.7/dist-packages/twocaptchaapi/__init__.py", line 62, in proxy
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/twocaptchaapi/__init__.py", line 75, in proxy
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/twocaptchaapi/__init__.py", line 147, in solve
raw_data = file.read()
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 632, in __getattr__
raise AttributeError(name)
AttributeError: read
imagebuf
不是一个。不能在with
语句中使用它,该语句通过首先存储。也没有必要关闭它
cptapi.solve
需要类似文件的对象;从:
将验证码排队等待解决<代码>文件可以是路径或文件对象
此处传入图像
对象没有意义,只需传入StringIO
实例即可:
imagefile = cStringIO.StringIO(urllib.urlopen(e.captcha_img).read())
captcha = cptapi.solve(imagefile)
同样,这里不需要使用close来关闭该对象,您不需要将与
一起使用
imagefile = cStringIO.StringIO(urllib.urlopen(e.captcha_img).read())
captcha = cptapi.solve(imagefile)