将Content

编程入门 行业动态 更新时间:2024-10-28 09:15:05
本文介绍了将Content-Type标头转换为文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我想做的是将HEADER请求内容类型转换为文件扩展名。对于html页面 text / html; charset = utf-8,典型的内容类型是这样的,这是python给出的响应。我一直在研究使用mimetype模块没有成功,因为它看起来无法满足我的需求。

运行时间:

我想将 text / html; charset = utf-8转换为该 .html

典型的图像内容类型取决于图像类型,其为 image / jpeg,但是我对图像不太担心,因为大多数url在路径中指定了图像。

我不想使用基本python库中没有的任何库。

p>

解决方案

您可以拆分并剥离:

r = requests.get( stackoverflow/questions/29674905/convert-content-type-header-into-file-extension) 来自mimetypes导入guess_extension print(guess_extension(r.headers ['content-type']。partition(';')[0] .strip())) .htm

So what I am trying to do is convert a HEADER requests content-type into a file extension. The typical content-type is like this for html pages "text/html; charset=utf-8" that is the given response from 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.

Rundown:

I want to convert "text/html; charset=utf-8" into this ".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. This is more for websites that don't end in "blahahah.html"

I do not want to use any libraries that are not in the base python library.

解决方案

You could split and strip:

r = requests.get("stackoverflow/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

更多推荐

将Content

本文发布于:2023-05-28 12:26:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/320863.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file 'D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!