BeautifulSoup是一个Python库,用于从HTML和XML文件中提取数据。它提供了一些搜索方法来查找特定标记或文本的内容。
find() 方法在文档中查找单个标记,并返回它的第一个匹配项。如果没有找到匹配项,则返回 None。
示例代码:
from bs4 import BeautifulSoup
html_doc = """
My paragraph.
"""soup = BeautifulSoup(html_doc, 'html.parser')
标记
p_tag = soup.find('p') print(p_tag)
h1_tag = soup.find('h1') print(h1_tag)
find_all() 方法在文档中查找所有匹配项,并将它们作为列表返回。
示例代码:
标记
p_tags = soup.find_all('p') print(p_tags)
h1_tags = soup.find_all('h1') print(h1_tags)