使用BeautifulSoup从text / html文档中获取干净的文本

时间:2012-03-18 19:56:03

标签: python unicode beautifulsoup imaplib

我有一个包含两种内容类型的文档:text / xml和text / html。我想使用BeautifulSoup来解析文档,最后得到一个干净的文本版本。文档以元组开头,所以我一直使用repr将它变成BeautifulSoup识别的东西,然后使用find_all通过搜索div找到文档的text / html位,如下所示:

soup = BeautifulSoup(repr(msg_data))
text = soup.html.find_all("div")

然后,我将文本转换回字符串,将其保存到变量中,然后将其转换回汤对象并在其上调用get_text,如下所示:

str_text = str(text)
soup_text = BeautifulSoup(str_text)
soup_text.get_text()

但是,然后将编码更改为unicode,如下所示:

u'[9:16 PM\xa0Erica: with images, \xa0\xa0and that seemed long to me anyway, 9:17     
PM\xa0me: yeah, \xa0Erica: so feel free to make it shorter, \xa0\xa0or rather, please do, 
9:18 PM\xa0nobody wants to read about that shit for 2 pages, \xa0me: :), \xa0Erica: while 
browsing their site, \xa0me: srsly, \xa0Erica: unless of course your writing is magic, 
\xa0me: My writing saves drowning puppies, \xa0\xa0Just plucks him right out and gives 
them a scratch behind the ears and some kibble, \xa0Erica: Maine is weird, \xa0me: haha]'

当我尝试将其重新编码为UTF-8时,如下所示:

soup.encode('utf-8')

我回到未解析的类型。

我想达到保存为字符串的干净文本然后我可以在文本中找到特定内容(例如,上面文本中的“小狗”)。

基本上,我在这里乱跑。有人可以帮忙吗?一如既往,非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:2)

编码没有破坏;它应该是它应该是什么。对于不间断的空间,'\xa0'是Unicode。

如果要将此(Unicode)字符串编码为ASCII,可以告诉编解码器忽略它不理解的任何字符:

>>> x = u'[9:16 PM\xa0Erica: with images, \xa0\xa0and that seemed long to me anyway, 9:17 PM\xa0me: yeah, \xa0Erica: so feel free to make it shorter, \xa0\xa0or rather, please do,  9:18 PM\xa0nobody wants to read about that shit for 2 pages, \xa0me: :), \xa0Erica: while  browsing their site, \xa0me: srsly, \xa0Erica: unless of course your writing is magic,  \xa0me: My writing saves drowning puppies, \xa0\xa0Just plucks him right out and gives  them a scratch behind the ears and some kibble, \xa0Erica: Maine is weird, \xa0me: haha]'
>>> x.encode('ascii', 'ignore')
'[9:16 PMErica: with images, and that seemed long to me anyway, 9:17 PMme: yeah, Erica: so feel free to make it shorter, or rather, please do,  9:18 PMnobody wants to read about that shit for 2 pages, me: :), Erica: while  browsing their site, me: srsly, Erica: unless of course your writing is magic,  me: My writing saves drowning puppies, Just plucks him right out and gives  them a scratch behind the ears and some kibble, Erica: Maine is weird, me: haha]'

如果你有时间,你应该看看Ned Batchelder最近的视频Pragmatic Unicode。它会让一切都清晰简单!