目录
一、获取元素节点的父节点
BeautifulSoup通过tag.parent获取tag节点的父节点,其中根节点
<html>
的父节点是名称为[document]的节点,这个[document]节点的父节点是None。
1.1、找出文档中指定元素节点的所有父节点的名称。
*_题一、找出文档中<p class="title"><b>The Dormouse's story</b></p>
的<b>
元素节点的所有父节点的名称。
from bs4 import BeautifulSoup
bor='''
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">
Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.
</p>
<p class="story">...</p>
</body>
</html>
'''
sou=BeautifulSoup(bor,"lxml")