【问题标题】:beautifulsoup add <div> with class at end of htmlBeautifulsoup 在 html 末尾添加 <div> 和类
【发布时间】:2021-03-05 16:29:38
【问题描述】:

我有一个带有多个 标签的 HTML 文件(在 div 中也有多个 div)。我想在特定位置的 HTML 末尾添加一个新的 标记和类。我也尝试了 append、insert 和 insert_after/insert_before,但是它并没有像我预期的那样工作。

我的 html 输入是:

   <div id="page">
   <div id="records">
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content</p>
   </div>
   </div>
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display again once
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content again once</p>
   </div>
   </div>
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display second time
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content second time</p>
   </div>
   </div>
</div>
</div>

我想在 &lt;div id="records"&gt; 的结束标签之前添加新的&lt;div&gt; 标签,末尾带有class="record"。

输出如下所示:

   <div id="page">
   <div id="records">
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content</p>
   </div>
   </div>
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display again once
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content again once</p>
   </div>
   </div>
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display second time
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content second time</p>
   </div>
   </div>
   <div class="record">
   <div class="header">
   <div class="title">
    Something here to display 3rd time
   </div>
   </div>
   <div class="disclaimer">
   <p>Here i want to print content 3rd time</p>
   </div>
   </div>
</div>
</div>

在我的例子中,&lt;div class="record"&gt; 的数量不是固定的,数量可能会一直变化。

我想在 python 中使用 BeautifulSoup 获得这个问题的解决方案/建议。

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    你可以在soup.find_all('div', class_='record')的最后一项之后使用insert_after:

    from bs4 import BeautifulSoup
    
    html = '<div id="records"> <div class="record"> <div class="header"> <div class="title"> Something here to display </div> </div> <div class="disclaimer"> <p>Here i want to print content</p> </div> </div> <div class="record"> <div class="header"> <div class="title"> Something here to display again once </div> </div> <div class="disclaimer"> <p>Here i want to print content again once</p> </div> </div> <div class="record"> <div class="header"> <div class="title"> Something here to display second time </div> </div> <div class="disclaimer"> <p>Here i want to print content second time</p> </div> </div> </div>'
    
    soup = BeautifulSoup(html, 'html.parser')
    
    extra_html = '''
    <div class="record">
        <div class="header">
            <div class="title">
                Something here to display 3rd time
            </div>
        </div>
        <div class="disclaimer">
            <p>Here i want to print content 3rd time</p>
        </div>
    </div>'''
    
    soup.find_all('div', class_='record')[-1].insert_after(BeautifulSoup(extra_html, 'html.parser')) # [-1] selects the last item
    

    输出print(soup.prettify()):

    <div id="records">
     <div class="record">
      <div class="header">
       <div class="title">
        Something here to display
       </div>
      </div>
      <div class="disclaimer">
       <p>
        Here i want to print content
       </p>
      </div>
     </div>
     <div class="record">
      <div class="header">
       <div class="title">
        Something here to display again once
       </div>
      </div>
      <div class="disclaimer">
       <p>
        Here i want to print content again once
       </p>
      </div>
     </div>
     <div class="record">
      <div class="header">
       <div class="title">
        Something here to display second time
       </div>
      </div>
      <div class="disclaimer">
       <p>
        Here i want to print content second time
       </p>
      </div>
     </div>
     <div class="record">
      <div class="header">
       <div class="title">
        Something here to display 3rd time
       </div>
      </div>
      <div class="disclaimer">
       <p>
        Here i want to print content 3rd time
       </p>
      </div>
     </div>
    </div>
    

    【讨论】:

    • 这正是我想要的,非常感谢这种提取最后一个元素并在一行之后插入的最简单方法。
    【解决方案2】:

    使用.append(),需要选择父元素或者&lt;div id="page"&gt;

    newRecord = '''
    <div class="record">
      <div class="header">
        <div class="title">
          Something here to display 3rd time
        </div>
      </div>
      <div class="disclaimer">
        <p>Here i want to print content 3rd time</p>
      </div>
    </div>
    '''
    
    soup = BeautifulSoup(sourceHTML, 'html.parser')
    page = soup.select_one('#page')
    page.append(BeautifulSoup(newRecord, 'html.parser'))
    print(soup.prettify())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-19
      • 2011-01-05
      • 1970-01-01
      • 2017-06-15
      • 2021-07-18
      • 2020-08-25
      • 2020-09-24
      • 1970-01-01
      相关资源
      最近更新 更多