通过设置headers的Range参数,来指定获取response的大小
url = 'https://www.news.cn/politics/leaders/2023-10/15/c_1129917302.htm'
headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "zh-CN,zh;q=0.9",
"cache-control": "no-cache",
"pragma": "no-cache",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
}
start_byte = 0 # 起始字节位置
end_byte = 1000 # 结束字节位置,根据需要调整
# 构建请求头
headers.update({"Range": f"bytes={start_byte}-{end_byte}"})
response = requests.get(url, headers=headers)
print(response.text)
结果:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="publishid" content="1129917302.11.2.0"/><meta name="source" content="新华社"/><meta name="pageid" content="11324.11100.0.0.11254969.0.0.0.0.0.114882.1129917302"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta content="telephone=no" name="format-detection" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no" name="viewport" />
<link rel="stylesheet" href="http://lib.xinhuanet.com/common/reset.css" />
<link rel="stylesheet" href="http://lib.xinhuanet.com/swiper/swiper3.4.2/swiper.min.css" />
<link rel="stylesheet" href="http://www.news.cn/2021detail/css/detail.css" />
<link rel="stylesheet" href="http://www.news.cn/2021detail/css/detail-sanwu.css" />
<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script src="http://lib.xinhuanet.com/common/share.js"></script>
<script src="http://lib.xinhuanet.com/jquery/jquery1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="fix-ewm domPC">
<img src="http://www.xinhuanet.com/detail2020/images/ewm.png" width="94" alt="" />
<div class="fxd">
<span class="fxd-wx"></span>
<a href="javascript:void(0)" class="fxd-wb"></a>
<span class="fxd-xcx"></span>
<