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>
本文来自博客园,作者:大码王,转载请注明原文链接:https://www.cnblogs.com/huanghanyu/
posted on
浙公网安备 33010602011771号