python - 如何在 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
?
谢谢 !
解决方案
我终于选择了这个解决方案:
tags = [soup.findAll('h2')[0], soup.findAll('h3')[7:11], soup.findAll('h4')[:7]]
articles = [i for tag in tags for i in tag]
推荐阅读
- java - mvn archetype:生成选择的版本
- primeng - Turbo 表分页问题(PrimeNG 7)
- javascript - 检查站点是 HTTPS 还是 HTTP?
- javascript - Array.map 上的 Angular Typescript 正在返回“”this.array.map 不是函数”
- android - 如果用户已经登录,在我的 android 应用程序中使用 google 登录的问题
- firebase - Firebase --> 测试运行后生成的 test_result_1.xml 似乎已损坏
- angular - 使用模块中的变量在装饰器中添加自定义 CSS
- networkx - 在图表中寻找“气泡”
- python - Google Cloud Dataflow - WriteToBigQuery:“NoneType”对象没有属性“__getitem__”
- javascript - 返回没有给出预期的答案