使用Beautiful Soup Python模块用纯文本替换标签
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用Beautiful Soup Python模块用纯文本替换标签,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3743字,纯文字阅读大概需要6分钟。
内容图文
我正在使用Beautiful Soup从网页中提取“内容”.我知道有些人之前已经问了这个question,他们都指向了美丽的汤,这就是我开始使用它的方式.
我能够成功地获得大部分内容,但是我遇到了一些标记,这些标记是内容的一部分. (我开始的基本策略是:如果节点中有多个x-chars,那么它就是内容).我们以下面的html代码为例:
<div id="abc">
some long text goes <a href="/"> here </a> and hopefully it
will get picked up by the parser as content
</div>
results = soup.findAll(text=lambda(x): len(x) > 20)
当我使用上面的代码来获取长文本时,它会在标签处中断(标识的文本将从’并且希望……’开始).所以我尝试用纯文本替换标记,如下所示:
anchors = soup.findAll('a')
for a in anchors:
a.replaceWith('plain text')
上面的方法不起作用,因为Beautiful Soup将字符串作为NavigableString插入,当我使用带有len(x)>的findAll时会导致同样的问题. 20.我可以使用正则表达式首先将html解析为纯文本,清除所有不需要的标签,然后调用Beautiful Soup.但我想避免两次处理相同的内容 – 我正在尝试解析这些页面,以便我可以显示给定链接的内容片段(非常像Facebook Share) – 如果一切都是用Beautiful Soup完成的,我认为它会更快.
所以我的问题是:有没有办法’清除标签’并用’纯文本’替换它们使用Beautiful Soup.如果没有,最好的方法是什么?
谢谢你的建议!
更新:Alex的代码在示例示例中运行良好.我也试过各种边缘情况,它们都运行良好(下面的修改).所以我在现实生活中的网站上试了一下,然后我遇到了困扰我的问题.
import urllib
from BeautifulSoup import BeautifulSoup
page = urllib.urlopen('http://www.engadget.com/2010/01/12/kingston-ssdnow-v-dips-to-30gb-size-lower-price/')
anchors = soup.findAll('a')
i = 0
for a in anchors:
print str(i) + ":" + str(a)
for a in anchors:
if (a.string is None): a.string = ''
if (a.previousSibling is None and a.nextSibling is None):
a.previousSibling = a.string
elif (a.previousSibling is None and a.nextSibling is not None):
a.nextSibling.replaceWith(a.string + a.nextSibling)
elif (a.previousSibling is not None and a.nextSibling is None):
a.previousSibling.replaceWith(a.previousSibling + a.string)
else:
a.previousSibling.replaceWith(a.previousSibling + a.string + a.nextSibling)
a.nextSibling.extract()
i = i+1
当我运行上面的代码时,我收到以下错误:
0:<a href="http://www.switched.com/category/ces-2010">Stay up to date with
Switched's CES 2010 coverage</a>
Traceback (most recent call last):
File "parselink.py", line 44, in <module>
a.previousSibling.replaceWith(a.previousSibling + a.string + a.nextSibling)
TypeError: unsupported operand type(s) for +: 'Tag' and 'NavigableString'
当我查看HTML代码时,“保持最新状态..”之前没有任何兄弟姐妹(我没有看到以前的兄弟姐妹如何工作,直到我看到Alex的代码并根据我的测试看起来它正在寻找’文字’因此,如果没有先前的兄弟,我很惊讶它没有通过a.previousSibling的if逻辑是None和a; nextSibling是None.
能不能让我知道我做错了什么?
-ecognium
解决方法:
适用于您的具体示例的方法是:
from BeautifulSoup import BeautifulSoup
ht = '''
<div id="abc">
some long text goes <a href="/"> here </a> and hopefully it
will get picked up by the parser as content
</div>
'''
soup = BeautifulSoup(ht)
anchors = soup.findAll('a')
for a in anchors:
a.previousSibling.replaceWith(a.previousSibling + a.string)
results = soup.findAll(text=lambda(x): len(x) > 20)
print results
发出的
$python bs.py
[u'\n some long text goes here ', u' and hopefully it \n will get picked up by the parser as content\n']
当然,您可能需要更加小心,即,如果没有a.string,或者如果a.previousSibling为None,那么您将需要合适的if语句来处理此类极端情况.但我希望这个总体思路可以帮到你. (事实上??,你可能还希望合并下一个兄弟,如果它是一个字符串 – 不确定如何与你的启发式len(x)> 20一起玩,但是例如说你有两个9个字符的字符串,其中< a>在中间包含一个5个字符的字符串,也许你想把这个字体作为一个“23个字符的字符串”?我无法分辨,因为我不明白你的启发式的动机). 我想象除了< a>您还要删除其他标签,例如< b>或者< strong>,也许< p>和/或< br>等…?我想这也取决于你的启发式背后的实际想法是什么!
内容总结
以上是互联网集市为您收集整理的使用Beautiful Soup Python模块用纯文本替换标签全部内容,希望文章能够帮你解决使用Beautiful Soup Python模块用纯文本替换标签所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。