漂亮的Soup在html标记上迭代

2024-09-23 10:30:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下html代码

<section>
    <section>
        <h2>Title1</h2>
        <p>Text1</p>
        <p>Text1</p>
     </section>
  <section>
        <h2>Title2</h2>
        <p>Text2</p>
        <p>Text2</p>
     </section>
  <section>
        <h2>Title3</h2>
        <p>Text3</p>
        <p>Text3</p>
     </section>
  </section>
<section>
        <h2>Title2-1</h2>
        <p>Text2-1</p>
        <p>Text2-1</p>
</section>
<section>
        <h2>Title3-1</h2>
        <p>Text3-1</p>
        <p>Text3-1</p>
</section>
在一些章节中有小节,但有些没有。 我想获取子节和没有子节的节的内容 我正试图遍历这些子部分,以便在scrapy中创建索引。 我有以下代码:
class RUSpider(BaseSpider):
name = "ru"
allowed_domains = ["http://127.0.0.1:8000/"]
start_urls = [
    "http://127.0.0.1:8000/week2/1_am/#/",
    "http://127.0.0.1:8000/week1/1/",
    "http://127.0.0.1:8000/week3/1_am/"
]
rules = [
    Rule(SgmlLinkExtractor(), follow=True)
]

def parse(self, response):

    filename = response.url.split("/")[3]
    hxs = HtmlXPathSelector(response)
    divs = hxs.select('//div')
    sections = divs.select('//section').extract()
   # print sections.extract


 #class definition for scrapy and html selector

    for each in sections: #iterate over loop [above sections]
        soup = BeautifulSoup(each)
        sp= soup.prettify()
        elements = soup.findAll("section".split())
        print len(elements),'sublength'
        if len(elements ) > 1:
            for element in elements:
                for subelement in element:
                    print subelement,'element'
        else:
            item = RItem() # create Index Item
            item['html_content'] = each
            print each
            yield item

一些结果的格式是正确的,尽管一些没有子部分的部分被分解成单独的元素。

我要每一个部分单独。我的意思是因为一个部分有其他部分。我想循环这些部分,并得到他们各自,这样我可以保持循环的轨道。因为有些部分没有子部分,所以不需要循环遍历它们。

在美联有更好的方法吗? 我想要以下输出

    <section>
        <h2>Title1</h2>
        <p>Text1</p>
        <p>Text1</p>
     </section>
  <section>
        <h2>Title2</h2>
        <p>Text2</p>
        <p>Text2</p>
     </section>
  <section>
        <h2>Title3</h2>
        <p>Text3</p>
        <p>Text3</p>
     </section>
 
    <section>
            <h2>Title2-1</h2>
            <p>Text2-1</p>
            <p>Text2-1</p>
    </section>
    <section>
            <h2>Title3-1</h2>
            <p>Text3-1</p>
            <p>Text3-1</p>
    </section>

Tags: httpforresponsehtmlsectionh2elementseach
1条回答
网友
1楼 · 发布于 2024-09-23 10:30:40

检查这个方法。这是你提供的数据的一个泛型。

data = """
<section>
    <section>
        <h2>Title1</h2>
        <p>Text1</p>
        <p>Text1</p>
     </section>
  <section>
        <h2>Title2</h2>
        <p>Text2</p>
        <p>Text2</p>
     </section>
  <section>
        <h2>Title3</h2>
        <p>Text3</p>
        <p>Text3</p>
     </section>
  </section>
<section>
        <h2>Title2-1</h2>
        <p>Text2-1</p>
        <p>Text2-1</p>
</section>
<section>
        <h2>Title3-1</h2>
        <p>Text3-1</p>
        <p>Text3-1</p>
</section>
"""
from bs4 import BeautifulSoup

soup = BeautifulSoup(data)

sections = soup.find_all('section')


for each in sections: #iterate over loop [above sections]
    if each.find('section'):
        continue
    else:
        print each.prettify()

相关问题 更多 >