BeautifulSoup的find_All方法不能找到所有的块
创始人
2024-11-27 12:30:36
0

可以使用 find_all 方法的参数来指定要查找的标签或特定属性,也可以使用 find_parents 和 find_next_siblings 方法来查找其它的块。

代码示例:

from bs4 import BeautifulSoup

html = """

Title One

paragraph 1

paragraph 2

Title Two

paragraph 3

""" # 使用 find_all 方法查找所有的 p 标签,并打印它们的内容 soup = BeautifulSoup(html, 'html.parser') ps = soup.find_all('p') for p in ps: print(p.string) # 使用 find_all 方法的参数来指定查找的标签 soup = BeautifulSoup(html, 'html.parser') div = soup.find('div', class_='book') ps = div.find_all('p') for p in ps: print(p.string) # 使用 find_parents 方法找到所有包含 p 标签的元素,再使用 find_all 方法查找它们的兄弟元素 soup = BeautifulSoup(html, 'html.parser') ps = soup.find_all('p') for p in ps: for parent in p.find_parents(): siblings = parent.find_next_siblings() for sibling in siblings: if sibling.name == 'p': print(sibling.string)

相关内容

热门资讯

7透视(AAPOKER)软件透... 7透视(AAPOKER)软件透明挂辅助工具(软件透明挂)透视辅助(2023已更新)(哔哩哔哩);AA...
3分钟实锤!永州扯胡子器软件透... 3分钟实锤!永州扯胡子器软件透明挂辅助-永州扯胡子器原来真的有挂(2021已更新)1、ai辅助优化,...
俱乐部(德扑之星ai)果真是真... 俱乐部(德扑之星ai)果真是真的有挂!原来真的有挂(2025已更新)(哔哩哔哩)关于德扑之星ai的基...
第六ai插件(德州线上扑克)软... 第六ai插件(德州线上扑克)软件透明挂辅助APP(辅助挂)透视辅助(2023已更新)(哔哩哔哩);科...
两分钟普及!捞腌菜软件外挂辅助... 两分钟普及!捞腌菜软件外挂辅助-捞腌菜软件原来真的有挂(2025已更新)1、任何德州ai辅助神器的玩...
第四教程(aapoker设置)... 第四教程(aapoker设置)软件透明挂辅助挂(透视)透视辅助(2020已更新)(哔哩哔哩);玩家必...
线上(德扑之星功能)原来是真的... 线上(德扑之星功能)原来是真的有挂!其实真的有挂(2024已更新)(哔哩哔哩);大神普及一款德州ai...
8最新版(线上德州有后台)外挂... 8最新版(线上德州有后台)外挂透明挂辅助器安装(软件透明挂)透视辅助(2022已更新)(哔哩哔哩)是...
6分钟普及!中至九江讨赏软件透... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中打牌都...
外挂(WPK机器人)果真是真的... 外挂(WPK机器人)果真是真的有挂!原来真的有挂(2023已更新)(哔哩哔哩)是由北京得WPK机器人...