Is it possible to get only a few bytes from some url and then close the connection to urllib / urllib2? Or can it even be a part from the nth byte to the kth? There is a page on this page, and I donβt need to download the whole page, only part of it.
You can set the Range header to request a specific range of bytes, but you are dependent on the server to execute the request:
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