Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-请求HTTP范围不工作_Python_Python Requests - Fatal编程技术网

Python-请求HTTP范围不工作

Python-请求HTTP范围不工作,python,python-requests,Python,Python Requests,根据答案,我可以使用范围标题仅下载html页面的一部分,但使用以下代码: import requests url = "http://stackoverflow.com" headers = {"Range": "bytes=0-100"} # first 100 bytes r = requests.get(url, headers=headers) print r.text 我得到了整个html页面。为什么它不工作?如果Web服务器不支持范围标题,它将被忽略 尝试使用其他支持标头的

根据答案,我可以使用范围标题仅下载html页面的一部分,但使用以下代码:

import requests

url = "http://stackoverflow.com"
headers = {"Range": "bytes=0-100"}  # first 100 bytes

r = requests.get(url, headers=headers)

print r.text

我得到了整个html页面。为什么它不工作?

如果Web服务器不支持
范围
标题,它将被忽略

尝试使用其他支持标头的服务器,例如
tools.ietf.org

import requests

url = "http://tools.ietf.org/rfc/rfc2822.txt"
headers = {"Range": "bytes=0-100"}
r = requests.get(url, headers=headers)
assert len(r.text) <= 101  # not exactly 101, because r.text does not include header
导入请求
url=”http://tools.ietf.org/rfc/rfc2822.txt"
headers={“Range”:“bytes=0-100”}
r=requests.get(url,headers=headers)

assert len(r.text)我没有使用:

headers = {"Range": "bytes=0-100"} 

尝试使用以下方法:

import requests

# you can change the url
url = requests.get('http://example.com/')

print(url.text)

如果服务器不支持字节范围怎么办?这将获取整个内容
-根据您链接到的页面中的注释,您解决了问题吗?请求包很好,这里也很好。请不要只发布代码作为答案,还要解释代码的作用以及它如何解决问题。带有解释的答案通常更有帮助,质量更好,更容易吸引选票