如何从Python响应中提取文本,响应格式为text/htm

2024-10-06 12:29:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Python请求模块,对于API执行,响应是文本/html格式的。我需要从回复中提取一个特定的文本。你知道吗

{'Cache-Control': 'must-revalidate', 'Content-Type': 'text/html; charset=UTF-8', 'Date': 'Thu, 16 May 2019 01:57:47 GMT', 'Expires': 'Sat, 26 Jul 1997 05:00:00 GMT', 'Pragma': 'no-cache', 'Server': 'Apache', 'Set-Cookie': 'targetValue-fox=a%3X4%3F%7vu%4t0%3Bs%3A32%3A%34242342jkjk2342342kj42%22%3G%9K; expires=Thu, 16-May-2019 03:57:47 GMT;'Connection': 'keep-alive'}

我需要提取Set-Cookie的值。请告诉我怎么做。你知道吗


Tags: 模块文本apicachecookiehtml格式content
1条回答
网友
1楼 · 发布于 2024-10-06 12:29:48

从字典的内容来看,它们实际上看起来像头,如果它们是,则使用req.headers将它们放入字典中

import requests
req = requests.get(.....)
resp = req.headers

然后可以从resp字典中提取键Set-Cookie的值

print(resp.get("Set-Cookie"))

相关问题 更多 >