【问题标题】:Fetch first n bytes from the URL从 URL 中获取前 n 个字节
【发布时间】:2011-10-30 11:47:50
【问题描述】:

是否可以从某个 URL 中仅获取一些字节,然后关闭与 urllib/urllib2 的连接?或者甚至可能是从第 n 个字节到第 k 个字节的一部分?那边有一个页面,我不需要加载整个页面,只加载其中的一部分。

【问题讨论】:

    标签: python urllib2 urllib


    【解决方案1】:

    可以设置Range标头请求一定范围的字节,但是 您依赖服务器来处理请求:

    import urllib2
    req = urllib2.Request('http://www.python.org/')
    #
    # Here we request that bytes 18000--19000 be downloaded.
    # The range is inclusive, and starts at 0.
    #
    req.headers['Range']='bytes=%s-%s' % (18000, 19000)
    f = urllib2.urlopen(req)
    # This shows you the actual bytes that have been downloaded.
    content_range=f.headers.get('Content-Range')
    print(content_range)
    # bytes 18000-18030/18031
    

    【讨论】:

      猜你喜欢
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 2020-06-12
      • 2020-12-25
      • 2014-09-17
      • 1970-01-01
      相关资源
      最近更新 更多