2011-10-30 51 views
3

是否可以从某个URL获取大量字节,然后使用urllib/urllib2关闭连接?甚至可能是从第n个字节到第k个部分?这边有一个页面,我不需要加载整个页面,只有一页。从URL中获取前n个字节

回答

6

您可以设置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