我想知道我是否有可能使用漂亮的汤获得几行 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
来排列几行吗?
最佳答案
使用 BeautifulSoup 的 find_parent
/find_parents
方法。
传递 tr
作为父搜索项,['id']
将打印 id 值
id.find_parent('tr')['id']
>> '12590559'
关于python - 如何使用 BeautifulSoup 查找元素的父标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51107486/