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

时间:2010-08-15 05:26:39

标签: python beautifulsoup

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

someEl = soup.findAll(text = "some text")

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

<table...>

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

<table>

2 个答案:

答案 0 :(得分: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

findAllPrevious以及findParents the docs。{/ p>

答案 1 :(得分:1)

while someEl.name != "table":
    someEl = someEl.parent
# someEl is now the table