我有一個HTML編碼的字符串:'''<img class="size-medium wp-image-113"\ style="margin-left: 15px;" title="su1"\ src="http://blah.org/wp-content/uploads/2008/10/su1-300x194.jpg"\ alt="" width="300" height="194" />'''我想將其更改為:<img class="size-medium wp-image-113" style="margin-left: 15px;" title="su1" src="http://blah.org/wp-content/uploads/2008/10/su1-300x194.jpg" alt="" width="300" height="194" /> 我希望將其注冊為HTML,以便瀏覽器將其呈現為圖像,而不是顯示為文本。字符串的存儲方式是這樣的,因為我正在使用一種名為的網絡抓取工具BeautifulSoup,它將“掃描”網頁并從中獲取某些內容,然后以該格式返回字符串。我已經找到了如何在C#中而不是在Python中執行此操作。有人可以幫我嗎?
3 回答

函數式編程
TA貢獻1807條經驗 獲得超9個贊
使用標準庫:
HTML轉義
try:
from html import escape # python 3.x
except ImportError:
from cgi import escape # python 2.x
print(escape("<"))
HTML轉義
try:
from html import unescape # python 3.4+
except ImportError:
try:
from html.parser import HTMLParser # python 3.x (<3.4)
except ImportError:
from HTMLParser import HTMLParser # python 2.x
unescape = HTMLParser().unescape
print(unescape(">"))
添加回答
舉報
0/150
提交
取消