我很困惑为什么我不能从 FriendFeed 下载一些 JSON 响应的全部内容。使用 urllib2 .
>>> import urllib2
>>> stream = urllib2.urlopen('http://friendfeed.com/api/room/the-life-scientists/profile?format=json')
>>> stream.headers['content-length']
'168928'
>>> data = stream.read()
>>> len(data)
61058
>>> # We can see here that I did not retrieve the full JSON
... # given that the stream doesn't end with a closing }
...
>>> data[-40:]
'ce2-003048343a40","name":"Vincent Racani'
如何使用 urllib2 检索完整响应?
最佳答案
获取所有数据的最佳方式:
fp = urllib2.urlopen("http://www.example.com/index.cfm")
response = ""
while 1:
data = fp.read()
if not data: # This might need to be if data == "": -- can't remember
break
response += data
print response
原因是 .read()
不能保证返回整个响应,鉴于套接字的性质。我认为这已在文档(可能是 urllib
)中进行了讨论,但我找不到它。
关于python - urllib2 没有检索整个 HTTP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1824069/