[英]How to parse the value of Content-Type from an HTTP Header Response?
我的应用程序发出大量 HTTP 请求。 不编写正则表达式,如何解析Content-Type
标头值? 例如:
text/html; charset=UTF-8
对于上下文,这是我在互联网上获取东西的代码:
from requests import head
foo = head("http://www.example.com")
我期望的输出类似于mimetools 中的方法。 例如:
x = magic("text/html; charset=UTF-8")
将输出:
x.getparam('charset') # UTF-8
x.getmaintype() # text
x.getsubtype() # html
不幸的是, requests
没有给你一个解析内容类型的接口,而且这个东西的标准库有点乱。 所以我看到两个选项:
选项 1 :去使用python-mimeparse第三方库。
选项 2 :要将 mime 类型与charset
选项分开,您可以使用requests
用于在内部解析类型/编码的相同技术:使用cgi.parse_header
。
response = requests.head('http://example.com')
mimetype, options = cgi.parse_header(response.headers['Content-Type'])
其余的应该足够简单,可以用split
处理:
maintype, subtype = mimetype.split('/')
你的问题有点不清楚。 我假设您正在使用某种 Web 应用程序框架,例如 Django 或 Flask?
以下是如何使用 Flask 读取 Content-Type 的示例:
from flask import Flask, request
app = Flask(__name__)
@app.route("/")
def test():
request.headers.get('Content-Type')
if __name__ == "__main__":
app.run()
您的响应 ( foo
) 将有一个带有标题的字典。 尝试类似:
foo.headers.get('content-type')
或者打印foo.headers
以查看所有标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.