简体   繁体   English

将Content-Type标头转换为文件扩展名

[英]Convert Content-Type header into file extension

So what I am trying to do is convert a HEADER requests content-type into a file extension. 所以我要做的是将HEADER请求内容类型转换为文件扩展名。 The typical content-type is like this for html pages "text/html; charset=utf-8" that is the given response from python. 对于html页面“text / html; charset = utf-8”,典型的内容类型是这样的,这是来自python的给定响应。 I have looked into using the mimetype module with no success as it doesn't look like it accommodates what I am looking for. 我已经研究过使用mimetype模块没有成功,因为它看起来不适合我正在寻找的东西。

Rundown: 撞倒:

I want to convert "text/html; charset=utf-8" into this ".html" 我想将“text / html; charset = utf-8”转换为此“.html”

The typical image content-type is "image/jpeg" depending on the image type, but I am not too worried about images, given that most urls specify the image in the path. 典型的图像内容类型是“image / jpeg”,具体取决于图像类型,但我并不太担心图像,因为大多数网址指定路径中的图像。 This is more for websites that don't end in "blahahah.html" 这更适用于不以“blahahah.html”结尾的网站

I do not want to use any libraries that are not in the base python library. 我不想使用任何不在基本python库中的库。

You could split and strip: 你可以拆分和剥离:

r = requests.get("http://stackoverflow.com/questions/29674905/convert-content-type-header-into-file-extension")

from mimetypes import guess_extension

print(guess_extension(r.headers['content-type'].partition(';')[0].strip()))
.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM