用美丽的汤剥离HTML时保留空间 [英] Preserve space when stripping HTML with Beautiful Soup

查看:100
本文介绍了用美丽的汤剥离HTML时保留空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

合并。段2< blockquote>引用1< block引用>引用2< / p>< / html>
print html $ b $ soup = BeautifulSoup(html)
print u''join(soup.findAll(text = True))

此代码的输出是Para 1 WordsMerge。Para 2Quote 1Quote 2。

I don不希望第一段的最后一句与第二段的第一句话合并。
例如。 Para 1 Words Merge。Para 2 Quote 1 Quote 2。
这可以使用BeautifulSoup库实现吗?

解决方案

只需使用空格加入件即可:

  print u''.join(soup.findAll(text = True))


from BeautifulSoup import BeautifulSoup

html = "<html><p>Para 1. Words</p><p>Merge. Para 2<blockquote>Quote 1<blockquote>Quote 2</p></html>"
print html
soup = BeautifulSoup(html)
print u''.join(soup.findAll(text=True))

The out put of this code is "Para 1 WordsMerge. Para 2Quote 1Quote 2".

I don't want the last word of paragraph one merging with the first word of paragraph two. eg. "Para 1 Words Merge. Para 2 Quote 1 Quote 2". Can this be achieved using the BeautifulSoup library?

解决方案

Just join the pieces with a space:

print u' '.join(soup.findAll(text=True))

这篇关于用美丽的汤剥离HTML时保留空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆