……

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>

 

 posted on 2020-06-22 11:04  大码王  阅读(709)  评论(0)    收藏  举报
复制代码