AttributeError: __exit__ while buffering downloaded image on python 2.7
AttributeError: __exit__ while buffering downloaded image on python 2.7
我正在尝试使用 python 脚本自动识别验证码文件。
然而,经过几天的努力,我的功能似乎与预期的结果相去甚远。
此外,我得到的回溯信息不足以帮助我进一步调查。
这是我的函数:
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
imagebuf
不是 context manager. You can't use it in a with
statement, which tests for a context manager by first storing the contextmanager.__exit__
method。也不需要关闭。
cptapi.solve
需要类文件对象;来自 solve()
docstring:
Queues a captcha for solving. file
may either be a path or a file object.
在这里传递 Image
对象没有意义,只需传递 StringIO
实例即可:
imagefile = cStringIO.StringIO(urllib.urlopen(e.captcha_img).read())
captcha = cptapi.solve(imagefile)
再说一次,这里不需要使用close that object,你不需要使用with
.
我正在尝试使用 python 脚本自动识别验证码文件。 然而,经过几天的努力,我的功能似乎与预期的结果相去甚远。
此外,我得到的回溯信息不足以帮助我进一步调查。
这是我的函数:
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
imagebuf
不是 context manager. You can't use it in a with
statement, which tests for a context manager by first storing the contextmanager.__exit__
method。也不需要关闭。
cptapi.solve
需要类文件对象;来自 solve()
docstring:
Queues a captcha for solving.
file
may either be a path or a file object.
在这里传递 Image
对象没有意义,只需传递 StringIO
实例即可:
imagefile = cStringIO.StringIO(urllib.urlopen(e.captcha_img).read())
captcha = cptapi.solve(imagefile)
再说一次,这里不需要使用close that object,你不需要使用with
.