find 与 find_all

58.3 find 与 find_all

find 返回第一个匹配;find_all 返回列表。

可按标签名、class、id 等属性筛选。

查找所有链接

from bs4 import BeautifulSoup
html = '<a href="/a">A</a><a href="/b">B</a>'
soup = BeautifulSoup(html, 'html.parser')
for a in soup.find_all('a'):
    print(a.get('href'), a.text)