如何用适当的unicode替换unicode-String中的HTML-实体?
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...ed,up。
所以问题是:如何处理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字符串。
发布于 2010-10-29 19:24:22
好吧,这个问题很傻,我得承认。我在交互式解释器中开发一个旧版本的rows。我不知道它的内容有什么问题,但这是正确的代码:
from BeautifulSoup import BeautifulSoup
f = open('path_to_file','r')
lines = [i for i in f.readlines()]
soup = BeautifulSoup(''.join(lines))
rows = soup.findAll('tr')
allArticles = []
for row in rows:
l =[]
for r in row.findAll('td'):
l += [r.string]
allArticles+=[l]我真丢脸!
发布于 2010-10-29 18:08:36
我想你需要的是ICU音译器。我认为有一种方法可以将HTML实体音译为Unicode。
尝试音译符id Hex/XML-Any,它应该是您想要的。在演示页面上,您可以选择“插入示例:复合”,然后在“复合1”框中输入Hex/XML-Any,在框中添加一些输入数据,然后按"transform“。这有帮助吗?
有一个Python绑定,但我认为它没有得到很好的处理。
发布于 2010-10-29 18:24:01
htmlentitydefs.entitydefs["quot"]返回'"'
这是一本字典,它将实体转换成它们的实际特征。
从那时起,你应该可以轻松地继续下去了。
https://stackoverflow.com/questions/4054551
复制相似问题