用 BeautifulSoup 替换 html 标签

发布于 2024-09-26 23:27:06 字数 501 浏览 0 评论 0原文

我目前正在使用 BeautifulSoup 重新格式化一些 HTML 页面,但遇到了一些问题。

我的问题是原始 HTML 有这样的内容:

<li><p>stff</p></li>

以及

<li><div><p>Stuff</p></div></li>

使用

<li><div><p><strong>stff</strong></p></div><li>

BeautifulSoup 我希望消除 div 和 p 标签(如果存在),但保留 Strong 标签。

我正在浏览漂亮的汤文档,但找不到任何内容。 有想法吗?

谢谢。

I'm currently reformatting some HTML pages with BeautifulSoup, and I ran into bit of a problem.

My problem is that the original HTML has things like this:

<li><p>stff</p></li>

and

<li><div><p>Stuff</p></div></li>

as well as

<li><div><p><strong>stff</strong></p></div><li>

With BeautifulSoup I hope to eliminate the div and the p tags, if they exists, but keep the strong tag.

I'm looking through the beautiful soup documentation and couldn't find any.
Ideas?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白况 2024-10-03 23:27:06

这个问题可能涉及旧版本的 BeautifulSoup 因为使用 bs4 你可以简单地使用 展开 函数:

s = BeautifulSoup('<li><div><p><strong>stff</strong></p></div><li>')
s.div.unwrap()
>> <div></div>
s.p.unwrap()
>> <p></p>
s
>> <html><body><li><strong>stff</strong></li><li></li></body></html>

This question probably refered to an older version of BeautifulSoup because with bs4 you can simply use the unwrap function:

s = BeautifulSoup('<li><div><p><strong>stff</strong></p></div><li>')
s.div.unwrap()
>> <div></div>
s.p.unwrap()
>> <p></p>
s
>> <html><body><li><strong>stff</strong></li><li></li></body></html>
风透绣罗衣 2024-10-03 23:27:06

您想要做的事情可以使用 replaceWith 来完成。您必须复制要用作替换的元素,然后将其作为参数提供给 replaceWithreplaceWith 的文档 非常清楚如何做到这一点。

What you want to do can be done using replaceWith. You have to duplicate the element you want to use as the replacement, and then feed that as the argument to replaceWith. The documentation for replaceWith is pretty clear on how to do this.

娇俏 2024-10-03 23:27:06

我看到了这个简单问题的很多答案,我也来这里看看一些有用的东西,但不幸的是我没有得到我正在寻找的东西,然后经过几次尝试我找到了这个答案的简单解决方案,这里是

soup = BeautifulSoup(htmlData, "html.parser")

h2_headers = soup.find_all("h2")

for header in h2_headers:
    header.name = "h1" # replaces h2 tag with h1 

所有 h2 标签转换为h1。您只需更改名称即可转换任何标签。

I saw many answers for this simple question, i also came here to see something useful but unfortunately i didn't get what i was looking for then after few tries I found a simple solution for this answer and here it is

soup = BeautifulSoup(htmlData, "html.parser")

h2_headers = soup.find_all("h2")

for header in h2_headers:
    header.name = "h1" # replaces h2 tag with h1 

All h2 tags converted to h1. You can convert any tag by just changing the name.

摘星┃星的人 2024-10-03 23:27:06

您可以编写自己的函数来剥离标签:

import re

def strip_tags(string):
    return re.sub(r'<.*?>', '', string)

strip_tags("<li><div><p><strong>stff</strong></p></div><li>")
'stff'

You can write your own function to strip tags:

import re

def strip_tags(string):
    return re.sub(r'<.*?>', '', string)

strip_tags("<li><div><p><strong>stff</strong></p></div><li>")
'stff'
千鲤 2024-10-03 23:27:06

简单的解决方案让整个节点意味着 div

  1. 转换为字符串
  2. 替换为所需的标签/字符串。
  3. 将相应的标签替换为空字符串。
  4. 通过传递给 beautifulsoup 将转换后的字符串转换为可解析的字符串

    我为mint做了什么

    示例:

    A **-231至 231-1**

    sup = opt.sup 
        如果sup: //opt有sup标签那么
    
             //opts 转换为字符串。 
             opt = str(opts).replace("","^").replace("","") //替换
    
             //再次从字符串转换为漂亮的字符串。
             s = BeautifulSoup(opt, 'lxml')
    
             //操作后退出所需变量
             opts = s.find("div", class_="col-md-12 选项")
    

    输出:

    <前><代码>-2^31 到 2^31-1
    如果不进行操作,它会像这样(-231 到 231-1)

Simple solution get your whole node means div:

  1. Convert to string
  2. Replace <tag> with required tag/string.
  3. Replace corresponding tag with empty string.
  4. Convert the converted string to parsable string by passing to beautifulsoup

    What I have done for mint

    Example:

    <div class="col-md-12 option" itemprop="text">
    <span class="label label-info">A</span>
    
    **-2<sup>31</sup> to 2<sup>31</sup>-1**
    

    sup = opt.sup 
        if sup: //opt has sup tag then
    
             //opts converted to string. 
             opt = str(opts).replace("<sup>","^").replace("</sup>","") //replacing
    
             //again converted from string to beautiful string.
             s = BeautifulSoup(opt, 'lxml')
    
             //resign to required variable after manipulation
             opts = s.find("div", class_="col-md-12 option")
    

    Output:

    -2^31 to 2^31-1
    without manipulation it will like this (-231 to 231-1)
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文