Python的 - HTTP请求范围不工作
问题描述:
根据this答案我可以使用Range头只下载一个html页面的一部分,但与此代码:Python的 - HTTP请求范围不工作
import requests
url = "http://stackoverflow.com"
headers = {"Range": "bytes=0-100"} # first 100 bytes
r = requests.get(url, headers=headers)
print r.text
我得到整个HTML页面。为什么它不工作?
答
如果网络服务器不支持Range
标题,它将被忽略。
尝试与支持头其他服务器,例如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
+0
请求包很好,这里工作也很好https://stackoverflow.com/questions/23602412/only-download-a-part-of-the-document-using-python-requests?answertab=oldest#tab-top –
'如果字节范围不被服务器支持什么?这将获取整个内容 - 根据您链接到 –
的页面中的评论你解决了吗? –