BeautifulSoup的find_all方法无法泛化
创始人
2024-11-27 12:30:46
0

可以使用Python的recursive函数,来实现find_all方法的泛化。

示例代码:

from bs4 import BeautifulSoup

def filter_tags(element): if element.has_attr('class') and 'news' in element['class']: return True else: return False

def find_all(root, filter_func): results = [] if filter_func(root): results.append(root) for child in root.children: if child.name is not None: results += find_all(child, filter_func) return results

html_doc = """ The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

...

"""

soup = BeautifulSoup(html_doc, 'html.parser')

使用filter_tags函数过滤class为news的标签

news_tags = find_all(soup.html, filter_tags)

for tag in news_tags: print(tag.prettify())

相关内容

热门资讯

线上(微扑克有挂)竟然是真的有... 线上(微扑克有挂)竟然是真的有挂!原来真的有挂(2022已更新)(哔哩哔哩)1、玩家可以在微扑克有挂...
第2德州版(fish poke... 1、第2德州版(fish poker外挂)外挂辅助器安装(透视)透视辅助(2025已更新)(哔哩哔哩...
9最新(wpk必胜)外挂透明挂... 9最新(wpk必胜)外挂透明挂辅助挂(透视)透视辅助(2021已更新)(哔哩哔哩);wpk必胜软件透...
8分钟攻略!江西中至麻将神器软... 自定义系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你是想分享给你好...
外挂(菠萝德州)的确是真的有挂... 外挂(菠萝德州)的确是真的有挂!其实真的有挂(2020已更新)(哔哩哔哩);科技详细教程小薇《757...
第三小程序(aapOker)外... 第三小程序(aapOker)外挂辅助挂(辅助挂)透视辅助(2021已更新)(哔哩哔哩) 科技详细教程...
9分钟了解!胡乐麻将app外挂... 9分钟了解!胡乐麻将app外挂透视辅助-胡乐麻将app其实真的有挂(2025已更新)1)辅助挂:进一...
第8安卓版(线上wpk德州ai... 第8安卓版(线上wpk德州ai机器人)软件透明挂辅助机制(软件透明挂)透视辅助(2020已更新)(哔...
ai代打(wepoke脚本)其... ai代打(wepoke脚本)其实是真的有挂!原来真的有挂(2025已更新)(哔哩哔哩)1、点击下载安...
五AI(德扑之星规律)外挂透明... 这是一款非常优秀的德扑之星规律 ia辅助检测软件,能够让你了解到德扑之星规律中牌率当中全部隐藏参数,...