Python StreamConsumerRor()异常由“引发”;对于响应中的块。iter_内容(1024)";

Python StreamConsumerRor()异常由“引发”;对于响应中的块。iter_内容(1024)";,python,python-2.7,exception,python-requests,Python,Python 2.7,Exception,Python Requests,我不确定问这个问题最好的方式是什么 但我正在编写一个脚本,使用request.get()从internet下载一些内容,一切正常,但由于某种原因,现在出现了StreamConsumerRor(),我不知道为什么 Traceback (most recent call last): File "./pythonDL-ver_0.0.4.py", line 90, in <module> for chunk in response.iter_content(1024): File "/L

我不确定问这个问题最好的方式是什么

但我正在编写一个脚本,使用request.get()从internet下载一些内容,一切正常,但由于某种原因,现在出现了StreamConsumerRor(),我不知道为什么

Traceback (most recent call last):
File "./pythonDL-ver_0.0.4.py", line 90, in <module>
for chunk in response.iter_content(1024):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 766, in iter_content
raise StreamConsumedError()
requests.exceptions.StreamConsumedError
回溯(最近一次呼叫最后一次):
文件“/pythonDL-ver_0.0.4.py”,第90行,in
对于响应中的块。iter_内容(1024):
iter_内容中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/requests/models.py”,第766行
提高StreamConsumerRor()
requests.exceptions.StreamConsumerError
如果你需要更多的信息,我可以添加进去。谢谢

while True:
    count = count + 1
    print 'testing ' + str(count) + '\n'
    print url
    url_2 = url.format(count = count)
    print '////' + str(url_2) + ' for loop ' + str(count) + "////\n"


    print str(url_2) + ' testing url \n'

    filename = posixpath.basename(url_2)
    print str(filename) + ' testing filename \n'
    response = requests.get(url_2, stream = True)
    responseString = str(response)

    print str(responseString) + 'testing res2 \n'

    if responseString == '<Response [404]>':
        print '......No more Requests......\n'
        break
    elif responseString == '<Response [200]>':
        print '......Successful Request....\n'
    else:
        break

    print responseString

    while responseString == '<Response [200]>':
        print 'testing while loop: ' + str(responseString)
        with open(filename, 'wb') as fp:
            for chunk in response.iter_content(1024):
                fp.write(chunk)

    count += 1
    print str(count) + ' = counting value'
为True时:
计数=计数+1
打印“测试”+str(计数)+'\n'
打印url
url_2=url.format(count=count)
为循环“+str(计数)+”打印“///”+str(url_2)+”
打印str(url_2)+“测试url\n”
filename=posixpath.basename(url_2)
打印str(文件名)+“测试文件名\n”
response=requests.get(url_2,stream=True)
responseString=str(响应)
打印str(responseString)+“testing res2\n”
如果responseString='':
打印“……无更多请求……”\n
打破
elif responseString=='':
打印“…成功请求…”\n
其他:
打破
打印响应预算
而responseString=='':
打印“循环时测试:”+str(响应字符串)
打开(文件名为“wb”)作为fp时:
对于响应中的块。iter_内容(1024):
fp.write(块)
计数+=1
打印str(计数)+'=计数值'

这是事情停止工作的循环。

示例代码肯定会有帮助。我已经在事情开始出错的地方添加了循环,希望它有意义。您正在重复尝试循环相同响应的
iter\u内容。
。哦。。愚蠢的我,我没有注意到,它现在起作用了,谢谢,很棒的东西,谢谢纳扎里。也谢谢你的解释。
    while responseString == '<Response [200]>':
        print 'testing while loop: ' + str(responseString)
        with open(filename, 'wb') as fp:
            for chunk in response.iter_content(1024):
                fp.write(chunk)
    if responseString == '<Response [200]>':
        with open(filename, 'wb') as fp:
            for chunk in response.iter_content(1024):
                fp.write(chunk)