如何使用BeautifulSoup查找元素的父标签?

时间:2018-06-29 18:25:24

标签: python python-3.x beautifulsoup

我想知道,使用美丽的汤,是否有可能获得html行的几行:

<tr id="12590559" class="">
<td>
<span class="he16-1 tip-top" title="Cracker"></span>
</td>
<td>
cracker.crc
</td>

在该示例中,我想使用以下方式提取ID,但附带标题信息:

soup = BeautifulSoup(lista.content, "lxml")
id = soup.find(attrs={"title": "Cracker"})

我可以得到

<span class="he16-1 tip-top" title="Cracker"></span>

但是我也想获得id。我可以使用BeautifulSoup来排几行吗?

1 个答案:

答案 0 :(得分:2)

使用 BeautifulSoup find_parent / find_parents方法。

通过tr作为父级搜索项,而['id']将显示id值

id.find_parent('tr')['id']

>> '12590559'
相关问题