requests获取指定响应体的大小

通过设置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>
<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值