BeautifulSoup查找HTML元素
BeautifulSoup查找HTML元素
查找文档的元素是我们爬取网页信息的重要手段,BeautifulSoup提供了一系列的查找元素的方法,其中功能强大的fisoup=BeautifulSoup(doc,"lxml")
tags=("a")
for tag in tags:
print(tag)
程序结果找到3个<a>元素:
<a class="sister" href="" id="link1">Elsie</a>
<a class="sister" href="" id="link2">Lacie</a>
<a class="sister" href="" id="link3">Tillie</a>
例2-3-3:查找文档中的第一个<a>元素
from bs4 import BeautifulSoup
doc='''
<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="" class="sister" id="link1">Elsie</a>,
<a href="" class="sister" id="link2">Lacie</a> and
<a href="" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.
</p>
<p class="story">...</p>
</body>
</html>
'''
soup=BeautifulSoup(doc,"lxml")
tag=("a")
print(tag)
程序结果找到第一个<a>元素:
<a class="sister" href="" id="link1">Elsie</a>
例2-3-4:查找文档中class="title"的<p>元素
from bs4 import BeautifulSoup
doc='''
<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="" class="sister" id="link1">Elsie</a>,
<a href="" class="sister" id="link2">Lacie</a> and
<a href="" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.
</p>
<p class="story">...</p>
</body>
</html>
'''
soup=BeautifulSoup(doc,"lxml")
tag=("p",attrs={"class":"title"})
print(tag)
程序结果找到class="title"的<p>元素
<p class="title"><b>The Dormouse's story</b></p>
很显然如果使用:
tag=("p")
也能找到这个元素,因为它是文档的第一个<p>元素。
例2-3-5:查找文档中class="sister"的元素
from
Python爬虫程序设计KC23 来自淘豆网m.daumloan.com转载请标明出处.