我不完全确定提出这个问题的最佳方式是什么,
但是我正在编写一个脚本,使用request.get()从互联网上下载一些内容,一切正常,但由于某种原因现在出现了StreamConsumedError(),我不知道为什么。< / p>
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
如果您需要更多信息,我可以将其添加进去。谢谢。
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'
这是事情停止工作的循环。
答案 0 :(得分:1)
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)
此循环永远不会终止,一旦responseString
为<Response [200]>
,它就会永远停留,因为没有任何改变。
阻止循环永远存在的唯一因素是您可以多次从流式响应中读取。所以当循环再次尝试时,响应会抛出错误。
最简单的解决方法是将while
替换为if
if responseString == '<Response [200]>':
with open(filename, 'wb') as fp:
for chunk in response.iter_content(1024):
fp.write(chunk)