我正在尝试对一棵 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))
我是一名优秀的程序员,十分优秀!