假设我在HTML页面中引用了一个表格内的元素,如下所示:
someEl = soup.findAll(text = "some text")
我确定这个元素嵌入在一个表中,有没有办法找到父表而不必多次调用.parent?
<table...>
..
..
<tr>....<td><center><font..><b>some text</b></font></center></td>....<tr>
<table>
答案 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