在BeautifulSoup中如何迭代追加函数?

时间:2016-07-06 16:13:04

标签: python html beautifulsoup

this question开始,以下代码遍历两个h2之间的代码:

from bs4 import BeautifulSoup, Tag


data = """<h2><name>Main Section</name><content>bla bla bla</content></h2>
<p>Bla bla bla<p>
<h3>Subsection</h3>
<p>Some more info</p>

<h3>Subsection 2</h3>
<p>Even more info!</p>


<h2><name>Main Section 2</name><content>blah...</content></h2>
<p>bla</p>
<h3>Subsection</h3>
<p>Some more info</p>

<h3>Subsection 2</h3>
<p>Even more info!</p>"""


soup = BeautifulSoup(data)
for main_section in soup.find_all('h2'):
    for sibling in main_section.next_siblings:
        if not isinstance(sibling, Tag):
            continue
        if sibling.name == 'h2':
            break
        print(sibling)

如果我最后使用print(sibling),这可以很好地工作并迭代整个数据。但是,如果我使用append

,则单次运行后代码会中断
soup = BeautifulSoup(data)
    for main_section in soup.find_all('h2'):
        for sibling in main_section.next_siblings:
            if not isinstance(sibling, Tag):
                continue
            if sibling.name == 'h2':
                break
 -------->  main_section.content.append(sibling.extract())

内容中只包含下一个兄弟(即使我删除了extract()同样的事情)。输出是:

<h2><name>Main Section</name><content>bla bla bla<p>Bla bla bla</p></content></h2>
<h2><name>Main Section 2</name><content>blah...<p>bla</p></content></h2>

如果我再次运行代码,则下一个标记会包含在<content>...</content>标记

基本上,我想要包含主要部分的content标记内的所有数据和子部分。

我想要的输出是:

    <h2><name>Main Section</name><content>bla bla bla<p>Bla bla bla</p><h3>Subsection</h3><p>Some more info</p><h3>Subsection 2</h3><p>Even more info!</p></content></h2>

    <h2><name>Main Section 2</name><content>blah...<p>bla</p><h3>Subsection</h3><p>Some more info</p><h3>Subsection 2</h3><p>Even more info!</p></content></h2>
  1. 为什么迭代会在我使用追加时停止?
  2. 如何在两个主要标签之间追加所有标签?

1 个答案:

答案 0 :(得分:0)

将标记附加到新列表解决了我的问题。

soup = BeautifulSoup(data)
for main_section in soup.find_all('h2'):
    x = []
    for sibling in main_section.next_siblings:
        if not isinstance(sibling, Tag):
            continue
        if sibling.name == 'h2':
            break
        x.append(sibling)
    for y in x:
        main_section.append(y)

然后我可以将所有兄弟姐妹添加到main_section