[英]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.