亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用Python / Django執行HTML解碼/編碼?

如何使用Python / Django執行HTML解碼/編碼?

守著一只汪 2019-11-23 13:22:22
我有一個HTML編碼的字符串:'''&lt;img class=&quot;size-medium wp-image-113&quot;\ style=&quot;margin-left: 15px;&quot; title=&quot;su1&quot;\ src=&quot;http://blah.org/wp-content/uploads/2008/10/su1-300x194.jpg&quot;\ alt=&quot;&quot; width=&quot;300&quot; height=&quot;194&quot; /&gt;'''我想將其更改為:<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("&gt;"))


查看完整回答
反對 回復 2019-11-23
  • 3 回答
  • 0 關注
  • 943 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號