需要使用 Beautiful Soup 提供的 find_all() 方法来获取所有匹配的标签。以下是一个示例代码:
from bs4 import BeautifulSoup
html = """
Beautiful Soup Example
Welcome to Beautiful Soup Example
Here's an example of how to extract data from HTML using Beautiful Soup.
- list item 1
- list item 2
- list item 3
"""
soup = BeautifulSoup(html, 'html.parser')
list_items = soup.find_all('li')
for item in list_items:
print(item.text)
在这个例子里,使用 find_all() 方法获取了所有的 li 标签,并使用循环打印出了每一个 list item。