python请求意外解码application/xgzip conten

2024-05-23 17:04:47 发布

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

我试图使用python请求检索https://donneespubliques.meteofrance.fr/donnees_libres/Txt/Synop/Archive/synop.201803.csv.gz。在

然而,当访问response的内容时,我得到的是csv数据,而不是我预期的gzip压缩的csv数据。我不清楚为什么。在

>>> url
'https://donneespubliques.meteofrance.fr/donnees_libres/Txt/Synop/Archive/synop.201803.csv.gz'
>>> resp = requests.get(url)
>>> resp.headers
{'Date': 'Thu, 19 Apr 2018 10:48:11 GMT', 'Server': 'MFWS', 
 'Last-Modified': 'Sat, 31 Mar 2018 21:10:09 GMT', 
 'ETag': '"3066bd-a2dce-568bbc81bee40"', 
 'Accept-Ranges': 'bytes', 
 'Content-Length': '667086', 
 'Content-Type': 'application/x-gzip', 
 'Content-Encoding': 'gzip',
 'Content-Disposition': 'attachment', 
 'Keep-Alive': 'timeout=5, max=300',
 'Connection': 'Keep-Alive'}
>>> resp.content[:100]
b'numer_sta;date;pmer;tend;cod_tend;dd;ff;t;td;u;vv;ww;w1;w2;n;nbas;hbas;cl;cm;ch;pres;niv_bar;geop;te'
>>> requests.__version__
'2.18.4'

如果我使用curl等访问同一个URL,则会得到预期的gzip内容:

^{pr2}$

这是我不懂的请求功能吗?服务器配置错误?在


Tags: csvhttpstxt内容contentfrresplibres
1条回答
网友
1楼 · 发布于 2024-05-23 17:04:47

这确实是一个特性,参见here。在

Encoded Data?

Requests automatically decompresses gzip-encoded responses, and does its best to decode response content to unicode when possible.

You can get direct access to the raw response (and even the socket), if needed as well.

相关问题 更多 >