python - urllib2 没有检索整个 HTTP 响应

标签 python http urllib2

我很困惑为什么我不能从 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/

相关文章:

python - Positional ,关键字,可选和必需参数之间的区别?

Python 3.6 类型检查 : numpy arrays and use defined classes

java - 在 Android 上请求发帖时出现问题

python - 使用 urllib3 进行多部分表单编码和发布

javascript - 获取 Python 脚本标签内的变量数据或从 js 添加的内容

python 在没有正则表达式的情况下在多个分隔符上拆分字符串

python - 如何使用 Pyspark 计算 RDD 上的平均值

http - 如何知道服务器是否支持 HTTP 1.0

http - Fiddler 不会捕获来自 Insomnia 的请求,但会捕获来自 Postman 的请求

python - 为什么我从 urllib2 发出的请求被 Apache2 延迟了?