使用beautifulsoup查找下一个同胞,直到某个同胞

使用beautifulsoup查找下一个同胞,直到某个同胞,第1张

使用beautifulsoup查找下一个同胞,直到某个同胞

我认为您可以执行以下 *** 作:

for section in soup.findAll('h2'):    nextNode = section    while True:        nextNode = nextNode.nextSibling        try: tag_name = nextNode.name        except AttributeError: tag_name = ""        if tag_name == "p": print nextNode.string        else: print "*****" break

鉴于:

<h2>section1</h2><p>article1</p><p>article2</p><p>article3</p><h2>section2</h2><p>article4</p><p>article5</p><p>article6</p>

输出

article1article2article3*****article4article5article6*****


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5663327.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存