Python 爬虫 – 根据id与class查找标签

本章介绍怎么根据id与class查找标签。假设有下面的HTML文档:

<html>
<head>
<title>A simple example page</title>
</head>
<body>
<div>
<p class="inner-text first-item" id="first">
First paragraph.
</p>
<p class="inner-text">
Second paragraph.
</p>
</div>
<p class="outer-text first-item" id="second">
<b>
First outer paragraph.
</b>
</p>
<p class="outer-text">
<b>
Second outer paragraph.
</b>
</p>
</body>
</html>

可以通过URL https://kevinhwu.github.io/demo/python-scraping/simple2.html 访问上面的文档。让我们先下载页面并创建一个BeautifulSoup对象:

import requests
from bs4 import BeautifulSoup

page = requests.get("https://kevinhwu.github.io/demo/python-scraping/simple2.html")
soup = BeautifulSoup(page.content, 'html.parser')

根据class查找标签

根据id与class查找标签,使用的仍旧是find_all方法。下面的例子,查找类是outer-textp标签:

soup.find_all('p', class_='outer-text')
[<p class="outer-text first-item" id="second">
<b>
First outer paragraph.
</b>
</p>, <p class="outer-text">
<b>
Second outer paragraph.
</b>
</p>]

在下面的例子中,查找任何类是outer-text的标签:

soup.find_all(class_="outer-text")
[<p class="outer-text first-item" id="second">
<b>
First outer paragraph.
</b>
</p>, <p class="outer-text">
<b>
Second outer paragraph.
</b>
</p>]

根据id查找标签

另外,也可以通过id查找标签:

soup.find_all(id="first")
[<p class="inner-text first-item" id="first">
First paragraph.
</p>]


浙ICP备17015664号 浙公网安备 33011002012336号 联系我们 网站地图  
@2019 qikegu.com 版权所有,禁止转载