簡體   English   中英

在python中解碼html編碼的字符串

[英]Decoding html encoded strings in python

我有以下字符串......

"Scam, hoax, or the real deal, he’s gonna work his way to the bottom of the sordid tale, and hopefully end up with an arcade game in the process."

我需要把它變成這個字符串......

騙局,惡作劇或真正的交易,他將努力工作到骯臟的故事的底部,並希望在這個過程中最終得到一個街機游戲。

這是非常標准的HTML編碼,我不能為我的生活弄清楚如何在python中轉換它。

我發現了這個: GitHub

並且它非常接近工作,但它不輸出撇號,而是輸出一些unicode字符。

以下是GitHub腳本輸出的示例...

騙局,惡作劇或真正的交易,他將在骯臟的故事的底部工作,並希望最終在這個過程中的街機游戲。

您正在嘗試做什么稱為“HTML實體解碼”,它包含在許多過去的Stack Overflow問題中,例如:

這是使用Beautiful Soup HTML解析庫解碼您的示例的代碼段:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from BeautifulSoup import BeautifulSoup

string = "Scam, hoax, or the real deal, he’s gonna work his way to the bottom of the sordid tale, and hopefully end up with an arcade game in the process."
s = BeautifulSoup(string,convertEntities=BeautifulSoup.HTML_ENTITIES).contents[0]
print s

這是輸出:

騙局,惡作劇或真正的交易,他將努力工作到骯臟的故事的底部,並希望在這個過程中最終得到一個街機游戲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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