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()
输出
' Here is some simple content for this page. '
find
另外,还有find
方法,返回同类标签的首个实例,返回结果是一个BeautifulSoup
对象:
soup.find('p')
输出
<p> Here is some simple content for this page. </p>