我正在尝试通过此 link 从网站检索 CSV 数据.
手动下载时,您会得到 synop.201708.csv.gz,它实际上是一个错误命名为 .gz 的 csv,它的大小为 2233KB
运行这段代码时:
import urllib
file_date = '201708'
file_url = "https://donneespubliques.meteofrance.fr/donnees_libres/Txt/Synop/Archive/synop.{}.csv.gz".format(file_date)
output_file_name = "{}.csv.gz".format(file_date)
print "downloading {} to {}".format(file_url, output_file_name)
urllib.urlretrieve (file_url, output_file_name)
我得到一个损坏的 ~361Kb 文件
有什么想法吗?
请您参考如下方法:
似乎正在发生的事情是 MétéoFrance 网站滥用了 Content-Encoding header 。该网站报告说它正在为您提供一个 gzip 文件 (Content-Type: application/x-gzip) 并且它正在将其编码为 gzip 格式以便传输 (Content-Encoding: x -gzip).它还说该页面是一个附件,应以其正常名称(Content-Disposition: attachment)保存
在真空中,这将是有意义的(在某种程度上;压缩一个已经压缩的文件大多是无用的):服务器提供一个 gzip 文件并再次压缩它以进行传输。收到后,您的浏览器会取消传输压缩并保存原始 gzip 文件。在这里,它解压缩流,但由于它没有再次压缩,所以它没有按预期工作。
