【问题标题】:urllib2.urlopen(): getting the size of the contenturllib2.urlopen():获取内容的大小
【发布时间】:2012-09-07 11:44:35
【问题描述】:

只要工作允许,我仍然会围绕 python 工作......

我正在使用使用 urllib2.urlopen 的脚本查询大量内部 webUI。我想知道如何从每个请求中获取页面内容的大小。我好像搞不懂这个。

提前致谢,

Mhibbin

【问题讨论】:

    标签: python urllib2


    【解决方案1】:
    print len(urlopen(url).read())
    

    >>> result = urllib2.urlopen('http://www.spiegel.de')
    >>> result.headers['content-length']
    '181291'
    

    【讨论】:

    • 对于内容的大小,urlopen(url).read().__sizeof__() 也可以。
    • 另外,这将耗尽调用了read() 的资源。如果您需要响应的 read() 结果及其大小,请务必同时调用两者,而不是像此处所做的那样在一个操作中调用。
    • 为了完整起见,第二个应该更快,因为您不必读取资源,而且确实不会耗尽资源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多