gpt4 book ai didi

python - 美汤如何轻松做广度优先搜索?

转载 作者:太空宇宙 更新时间:2023-11-04 05:04:47 25 4
gpt4 key购买 nike

我正在尝试对一棵 BeautifulSoup 树进行呼吸优先搜索。我知道,我们可以像这样用 Beautiful soup 进行深度优先搜索:

html = """SOME HTML FILE"""

soup = BeautifulSoup(html)

for child in soup.recursiveChildGenerator():
# do some stuff here
pass

但我不知道如何进行呼吸优先搜索,有人有任何想法和建议吗?

感谢您的帮助。

最佳答案

使用the .children generator对于要附加到广度优先队列的每个元素:

from bs4 import BeautifulSoup
import requests

html = requests.get("https://stackoverflow.com/questions/44798715/").text
soup = BeautifulSoup(html, "html5lib")
queue = [([], soup)] # queue of (path, element) pairs
while queue:
path, element = queue.pop(0)
if hasattr(element, 'children'): # check for leaf elements
for child in element.children:
queue.append((path + [child.name if child.name is not None else type(child)],
child))
# do stuff
print(path, repr(element.string[:50]) if element.string else type(element))

关于python - 美汤如何轻松做广度优先搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44798715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com