|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> from bs4 import BeautifulSoup as BS
>>> html_doc = """<html><head></head>
<body>
<p class="title"><b>An Interesting Story</b></p>
<p class="story">Long long age, there lives an interesting man. He lives with
<a class="father" id="link1">father</a>
<a class="mother> id="link2">mother</a>
<a class="brother" id="link3">brother</a>and
<a class="sister" id="link4">sister</a>.
They live in harmony...</p>
<p class="story">...</p>
</body>
"""
>>> soup = BS(html_doc, 'lxml')
>>> tag = soup.p.b
>>> for parent in tag.parents:
print(parent)
<p class="title"><b>An Interesting Story</b></p>
<body>
<p class="title"><b>An Interesting Story</b></p>
<p class="story">Long long age, there lives an interesting man. He lives with
<a class="father" id="link1">father</a>
<a class="mother> id=" link2="">mother</a>
<a class="brother" id="link3">brother</a>and
<a class="sister" id="link4">sister</a>.
They live in harmony...</p>
<p class="story">...</p>
</body>
<html><head></head>
<body>
<p class="title"><b>An Interesting Story</b></p>
<p class="story">Long long age, there lives an interesting man. He lives with
<a class="father" id="link1">father</a>
<a class="mother> id=" link2="">mother</a>
<a class="brother" id="link3">brother</a>and
<a class="sister" id="link4">sister</a>.
They live in harmony...</p>
<p class="story">...</p>
</body>
</html>
<html><head></head>
<body>
<p class="title"><b>An Interesting Story</b></p>
<p class="story">Long long age, there lives an interesting man. He lives with
<a class="father" id="link1">father</a>
<a class="mother> id=" link2="">mother</a>
<a class="brother" id="link3">brother</a>and
<a class="sister" id="link4">sister</a>.
They live in harmony...</p>
<p class="story">...</p>
</body>
</html> |
|