Python 爬虫 – 根据类型查找标签

find_all

如要查找全部同类标签,可以使用find_all方法。

import requests
from bs4 import BeautifulSoup

page = requests.get("https://kevinhwu.github.io/demo/python-scraping/simple.html")

soup = BeautifulSoup(page.content, 'html.parser')
soup.find_all('p')
[<p>
Here is some simple content for this page.
</p>]

find_all返回的是一个列表。可以使用列表索引获取某个标签:

soup.find_all('p')[0].get_text()
'\nHere is some simple content for this page.\n'

find

另外,还有find方法,返回同类标签的首个实例,返回结果是一个BeautifulSoup对象:

soup.find('p')
<p>
Here is some simple content for this page.
</p>


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