Skip to main content
 首页 » 编程设计

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

2025年02月15日7Free-Thinker

我正在尝试对一棵 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))