删除AppEngine Python Env中的HTML标记(相当于Ruby的Sanitize)

Eco*_*ium 1 python google-app-engine html-sanitizing

我正在寻找一个python模块,它将帮助我摆脱HTML标签,但保留文本值.之前我尝试过BeautifulSoup,我无法弄清楚如何完成这个简单的任务.我尝试搜索可以执行此操作的Python模块,但它们似乎都依赖于其他在AppEngine上运行不正常的库.

下面是Ruby的sanitize库中的示例代码,这就是我在Python中所追求的:

require 'rubygems'
require 'sanitize'

html = '<b><a href="http://foo.com/">foo</a></b><img src="http://foo.com/bar.jpg" />'

Sanitize.clean(html) # => 'foo'
Run Code Online (Sandbox Code Playgroud)

谢谢你的建议.

-e

Ale*_*lli 5

>>> import BeautifulSoup
>>> html = '<b><a href="http://foo.com/">foo</a></b><img src="http://foo.com/bar.jpg" />'
>>> bs = BeautifulSoup.BeautifulSoup(html)  
>>> bs.findAll(text=True)
[u'foo']
Run Code Online (Sandbox Code Playgroud)

这为您提供了(Unicode)字符串列表.如果要将其转换为单个字符串,请使用''.join(thatlist).