如何将文件从一个字节下载到另一个字节.(例如下载HTML文件的页脚)



如何将文件从一个字节下载到另一个字节。(例如下载HTML文件的页脚)使用python3??

谢谢。

在python中可以这样做:

import urllib2
def read_range(url, rstart, rstop):
    # First request url
    response = urllib2.urlopen(url)
    # Ignore content to start byte
    response.read(max(rstart-1,0))
    # Read bytes we want.
    return response.read(rstop - rstart)
# First 200 bytes.
print read_range("http://stackoverflow.com", 0, 200)
# Last 200 bytes
print read_range("http://stackoverflow.com", 200, 0)
# Some bytes right in the middle
print read_range("http://stackoverflow.com", 400, 1000)
# Read whole file
print read_range("http://stackoverflow.com", 0, -1)

这当然会发送一个200请求,没有任何特殊的头,但仍然是你所要求的。

如果服务器返回一个Accept-Ranges: bytes报头,这意味着你可以通过设置Range报头来请求特定的范围,就像这里的Range: bytes=1170-1246一样:

~: curl -v http://example.com/ -r 1170-1246
* Hostname was NOT found in DNS cache
*   Trying 93.184.216.119...
* Connected to example.com (93.184.216.119) port 80 (#0)
> GET / HTTP/1.1
> Range: bytes=1170-1246
> User-Agent: curl/7.37.0
> Host: example.com
> Accept: */*
> 
< HTTP/1.1 206 Partial Content
< Accept-Ranges: bytes
< Cache-Control: max-age=604800
< Content-Range: bytes 1170-1246/1270
< Content-Type: text/html
< Date: Tue, 03 Jun 2014 16:37:10 GMT
< Etag: "359670651"
< Expires: Tue, 10 Jun 2014 16:37:10 GMT
< Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
< Server: ECS (sea/F622)
< Connection: Keep-Alive
< 
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
* Connection #0 to host example.com left intact

如果没有,您就必须请求整个页面并使用通常的Python切片。

可以不先检查Accept-Ranges就发送Range报头;只是要确保区分200和206的回复。

最新更新