首页 > 解决方案 > 如何在 BeautifSoup 中传递 findAll 切片标签列表

问题描述

我现在可以传递一个标签列表,soup.findAll()soup.findAll(['h2', 'h3', 'h4,]) 对于其中的 2 个标签,我只对特定的标签感兴趣。在我的例子中 soup.findAll('h2')[0]soup.findAll('h3')[7:11]soup.findAll('h4')[:7]

有没有办法做到这一点,或者至少将特定的切片标签放在同一位置bs4.element.ResultSet

谢谢 !

标签: pythonweb-scrapingbeautifulsoupfindall

解决方案


我终于选择了这个解决方案:

tags = [soup.findAll('h2')[0], soup.findAll('h3')[7:11], soup.findAll('h4')[:7]]
articles = [i for tag in tags for i in tag]

推荐阅读