从汤 BeautifulSoup/Python 中删除 span 标签 [英] Removing span tags from soup BeautifulSoup/Python

查看:32
本文介绍了从汤 BeautifulSoup/Python 中删除 span 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的 Python 汤:

I have a soup in Python like this:

<p>
 <span style="text-decoration: underline; color: #3366ff;">
   Title:
 </span>
 Info
</p>
<p>
 <span style="color: #3366ff;">
  <span style="text-decoration: underline;">
   Title2:
  </span>
 </span>
 Info2
</p>

我想让它看起来像这样:

I'd like to get it to look like this:

<p>
   Title:
 Info
</p>
<p>
   Title2:
 Info2
</p>

有没有办法用 bs4 做到这一点?

Is there a way to do this with bs4?

推荐答案

你会想要使用 beautifulsoup 的 unwrap() 为此.

You'll be wanting to use beautifulsoup's unwrap() for this.

import bs4
soup1 = bs4.BeautifulSoup(htm1, 'html.parser')
for match in soup1.findAll('span'):
    match.unwrap()
print soup1

这篇关于从汤 BeautifulSoup/Python 中删除 span 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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