python怎么从一段httpresponse中提取出body和headers

python怎么从一段httpresponse中提取出body和headers

题目描述

通过proxy获取了网站的response,怎么把string格式的数据转换为对象呢

相关代码

HTTP/1.1 200 OK

Connection: close

Content-Length: 1362

Accept-Ranges: bytes

Cache-Control: public, max-age=86400

Content-Type: image/svg+xml

Date: Thu, 06 May 2021 06:08:37 GMT

Etag: "3e9241-552-600a79ad"

Last-Modified: Fri, 22 Jan 2021 07:07:25 GMT

X-Content-Type-Options: nosniff

X-Powered-By: webserver

X-Xss-Protection: 1; mode=block

<?xml version="1.0" encoding="UTF-8"?>

<svg viewBox="0 0 10 10" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

<!-- Generator: Sketch 53.2 (72643) - https://sketchapp.com -->

<title>删除 Normal</title>

<desc>Created with Sketch.</desc>

<g id="页面-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">

<g id="登录-copy" transform="translate(-1423.000000, -481.000000)" fill="#CCCCCC">

<g id="分组" transform="translate(1144.000000, 466.000000)">

<path d="M284,18.7364506 L287.474761,15.2616897 C287.82368,14.9127701 288.389391,14.9127701 288.73831,15.2616897 C289.08723,15.6106092 289.08723,16.1763195 288.73831,16.5252391 L285.263549,20 L288.73831,23.4747609 C289.08723,23.8236805 289.08723,24.3893908 288.73831,24.7383103 C288.389391,25.0872299 287.82368,25.0872299 287.474761,24.7383103 L284,21.2635494 L280.525239,24.7383103 C280.17632,25.0872299 279.610609,25.0872299 279.26169,24.7383103 C278.91277,24.3893908 278.91277,23.8236805 279.26169,23.4747609 L282.736451,20 L279.26169,16.5252391 C278.91277,16.1763195 278.91277,15.6106092 279.26169,15.2616897 C279.610609,14.9127701 280.17632,14.9127701 280.525239,15.2616897 L284,18.7364506 Z" id="删除-Normal"></path>

</g>

</g>

</g>

</svg>

你期待的结果是什么?实际看到的错误信息又是什么?

想从中提取出body但是找到的资料都是直接从request.get获取。很苦恼


回答:

如果是requests.get返回的Response对象,用response.text就可以。

如果有编码问题,就自己从response.content解码。

如果整个返回就是一个str,那只能用特征字符分割了


回答:

用requests库,请求头改为application/json再试一下,看结果resp.json()能否返回为字典

以上是 python怎么从一段httpresponse中提取出body和headers 的全部内容, 来源链接: utcz.com/p/937954.html

回到顶部