将Content-Typeheader转换成文件扩展名
Convert Content-Type header into file extension
所以我想做的是将 HEADER 请求 content-type 转换为文件扩展名。对于 html 页 "text/html; charset=utf-8",典型的 content-type 是这样的,这是 python 的给定响应。我已经研究过使用 mimetype 模块但没有成功,因为它看起来不像我正在寻找的东西。
破败:
我想将 "text/html; charset=utf-8" 转换成这个 ".html"
典型的图像 content-type 是 "image/jpeg" 取决于图像类型,但我不太担心图像,因为大多数 url 在路径中指定图像。这更适用于不以 "blahahah.html"
结尾的网站
我不想使用任何不在基础 python 库中的库。
你可以拆分和剥离:
r = requests.get("")
from mimetypes import guess_extension
print(guess_extension(r.headers['content-type'].partition(';')[0].strip()))
.htm
所以我想做的是将 HEADER 请求 content-type 转换为文件扩展名。对于 html 页 "text/html; charset=utf-8",典型的 content-type 是这样的,这是 python 的给定响应。我已经研究过使用 mimetype 模块但没有成功,因为它看起来不像我正在寻找的东西。
破败:
我想将 "text/html; charset=utf-8" 转换成这个 ".html"
典型的图像 content-type 是 "image/jpeg" 取决于图像类型,但我不太担心图像,因为大多数 url 在路径中指定图像。这更适用于不以 "blahahah.html"
结尾的网站我不想使用任何不在基础 python 库中的库。
你可以拆分和剥离:
r = requests.get("")
from mimetypes import guess_extension
print(guess_extension(r.headers['content-type'].partition(';')[0].strip()))
.htm