我正试图从一个使用python的网站上获取一些中文文本。当我得到它时,它被html标记包围,如下所示:
我今天的<em class="hot">心情</em>不好。<br/> I'm feeling blue today.
(我不得不把它作为代码来防止html标记消失)但是,一旦我使用切片来消除html标记,我得到:
我今天的心情ᄌヘ好。
为什么这个奇怪的角色出现在倒数第二位?谢谢你的帮助!
发布于 2016-02-25 05:25:14
使用regex模块,您可以使用\p{Han}过滤汉字:
>>> text = u'''我今天的<em class="hot">心情</em>不好。<br/> I'm feeling blue today.'''
>>> import regex
>>> print u''.join(regex.findall(r'\p{Han}+', text, flags=regex.UNICODE))
我今天的心情不好或者,使用unicodedata.name
>>> import unicodedata
>>> unicodedata.name(u'a')
'LATIN SMALL LETTER A'
>>> unicodedata.name(u'我')
'CJK UNIFIED IDEOGRAPH-6211'
>>> unicodedata.name(u'今')
'CJK UNIFIED IDEOGRAPH-4ECA'>>> text = u'''我今天的<em class="hot">心情</em>不好。<br/> I'm feeling blue today.'''
>>> print u''.join(c for c in text if unicodedata.name(c).startswith('CJK'))
我今天的心情不好https://stackoverflow.com/questions/35618631
复制相似问题