-2
我試圖自動化與python腳本的captcha文件識別。 但是,經過幾天的努力,我的功能似乎遠非預期的結果。AttributeError:__exit__緩衝下載的圖像在Python 2.7
此外,我得到的追蹤信息不足以幫助我進一步調查。
這裏是我的功能:
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__
有人請澄清正是我做錯了嗎?
而且我沒跟圖像處理無緩衝成功:
key = e.captcha_sid
response = requests.get(e.captcha_img)
img = Image.open(StringIO.StringIO(response.content))
with img as captcha_file:
captcha = cptapi.solve(captcha_file)
導致:
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
非常感謝。問題解決了。 – unexposed