Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AttributeError:_退出_,同时在python 2.7上缓冲下载的图像_Python_Image_Python 2.7_Python Imaging Library_Buffering - Fatal编程技术网

AttributeError:_退出_,同时在python 2.7上缓冲下载的图像

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

我正在尝试用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__
这导致:

    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)