簡體   English   中英

如何從 HTTP 標頭響應中解析 Content-Type 的值?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM