BeautifulSoup 不要在已找到的标签中递归搜索

BeautifulSoup dont search recursive in already found tag

我目前正在处理包含列表的维基百科文章。

我现在收到文章 List of Archdeacons in the Diocese in Europe。要获取 ul(或 table)标签,我使用 BeautifulSoup(Python 2.7)。 发现文章内容被'mw-content-text'标签包含:

article = soup.find('div', {'id':'mw-content-text'})
lists = article.find_all('ul')

这就是我获取列表的方式,它适用于此示例。但是有些文章的列表包含其他列表,我不希望 BS 跟踪这些子列表。我怎么说 BS 发现标签时我不必看得更深?

参数 recursive 不是我要找的,因为列表可能在 div 标记中。

P.S.: This 用户正在寻找相同的解决方案,但没有得到正确的答案:"Is there a way to make BS not search recursively into an already found tag?'"

一种可能的方法是使用 function to search for ul tags. For every ul tag found check that there is no parent ul tags found using find_parent():

article.find_all(lambda x: x.name == 'ul' and x.find_parent("ul") is None)