如何用unicode替換unicode-Strings中的HTML實體?如何處理UTF-8編碼的字符串和BeautifulSoup?
u'"HAUS Kleider" - Über das Bekleiden und Entkleiden, das VerhŸllen und Veredeln'
到
u'"HAUS-Kleider" - Über das Bekleiden und Entkleiden, das Verhüllen und Veredeln'
編輯
其實實體是錯誤的。看起來像BeautifulSoup f ...編輯它。
所以問題是:如何處理UTF-8編碼的字符串和BeautifulSoup?
from BeautifulSoup import BeautifulSoup
f = open('path_to_file','r')
lines = [i for i in f.readlines()]
soup = BeautifulSoup(''.join(lines))
allArticles = []
for row in rows:
l =[]
for r in row.findAll('td'):
l += [r.string] # here things seem to go wrong
allArticles+=[l]
Ü -> Ÿ
,而不是Ü
但實際上我不希望編碼仍然會改變。
>>> soup.originalEncoding
'utf-8'
,但我不能產生它的正確Unicode字符串
可能重複[在Python字符串中解碼HTML實體?](http://stackoverflow.com/questions/2087370/decode-html-entities-in-python-string) – geoffspear 2010-10-29 18:02:15
事情似乎出錯了? BeautifulSoup發起了它?這些實體是錯誤的?請嘗試提供更精確的詳細信息以使此問題可以回覆。 BeautifulSoup傾向於很好地處理UTF-8。 – 2010-10-29 18:20:23