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
>>> 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).
| 归档时间: |
|
| 查看次数: |
1440 次 |
| 最近记录: |