Find next siblings until a certain one using beautifulsoup

前端 未结 2 488
醉梦人生
醉梦人生 2020-12-19 03:37

The webpage is something like this:

section1

article

article

article

2条回答
  •  轮回少年
    2020-12-19 04:05

    The next_siblings iterator can be helpful here as well:

    for i in soup.find_all('h2'):
        for sib in i.next_siblings:
            if sib.name == 'p':
                print(sib.text)
            elif sib.name == 'h2':
                print ("*****")
                break
    

提交回复
热议问题