要获取所有匹配的元素,需要使用find_all()方法。例如:
from bs4 import BeautifulSoup
html_doc = """
The Dormouse's story
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...
"""
soup = BeautifulSoup(html_doc, 'html.parser')
all_links = soup.find_all('a')
for link in all_links:
print(link.get('href'))
输出:
http://example.com/elsie
http://example.com/lacie
http://example.com/tillie