【问题标题】:Replace a <div> tag to <h2> tag using BeautifulSoup [duplicate]使用 BeautifulSoup 将 <div> 标签替换为 <h2> 标签
【发布时间】:2014-03-09 09:51:19
【问题描述】:

我有很多 html 文件,我需要将所有span 或div 标记替换为class 的特定属性到h3 标记。

例如,

之前:

<div class="style1"> Text </div>

<span class="style1"> Text2 </span>

之后:

<h3> Text </h3>

<h3> Text2 </h3>

如何使用 BeautifulSoup 库做到这一点?

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    你可以设置一个 Tag 的 name 属性来改变它:

    bs = BeautifulSoup("<div>text</div>")
    bs.find("div").name = "h2"
    
    print bs # "<h2>text</h2>"
    

    【讨论】:

      猜你喜欢
      • 2015-03-26
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 2013-06-02
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多