Convert Content-Type header to file extension

Therefore, I am trying to convert a HEADER request for a content type to a file extension. A typical content type is similar to this for the html pages "text / html; charset = utf-8", which is a given response from python. I learned to use the mimetype module without any success, since it doesn't look like what I'm looking for.

Summary:

I want to convert "text / html; charset = utf-8" to this ".html"

A typical type of image content is "image / jpeg" depending on the type of image, but I'm not too worried about images, given that most URLs point the image in a path. This is more for sites that do not end in "blahahah.html"

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

+5
source share
1 answer

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 
+8
source

All Articles