使用BeautifulSoup,我可以快速遍历特定的父元素吗?

Bla*_*man 3 python beautifulsoup

假设我引用HTML页面中的表格内的元素,如下所示:

someEl = soup.findAll(text = "some text")
Run Code Online (Sandbox Code Playgroud)

我知道这个元素是嵌入在一个表中的,有没有办法找到父表而不必多次调用.parent?

<table...>

..
..
<tr>....<td><center><font..><b>some text</b></font></center></td>....<tr>

<table>
Run Code Online (Sandbox Code Playgroud)

Jes*_*lon 6

退房findParents,它有类似的形式findAll:

soup = BeautifulSoup("<table>...</table>")

for text in soup.findAll(text='some text')
  table = text.findParents('table')[0]
  # table is your now your most recent `<table>` parent
Run Code Online (Sandbox Code Playgroud)

以下是该文档findAllPrevious和也findParents.