html_doc ="""
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc,'html.parser')
1 过滤器
在介绍文档搜索方法之前,先了解下各种过滤器。
1.1 字符串
即在搜索方法中传如一个字符串参数;
BeautifulSoup会查找与字符串完全匹配的内容;
如查找b标签:
print(soup.find_all('b'))
输出为:
[<b>The Dormouse's story</b>]
1.2 正则表达式
传入正则表达式作为参数;
Beautiful Soup会通过正则表达式的 match() 来匹配内容;
如找出所有以b开头的标签:
import re
for tag in soup.find_all(re.compile("^b")):print(tag.name)
find_all_next( name , attrs , recursive , string ,**kwargs )
find_next( name , attrs , recursive , string ,**kwargs )
这2个方法通过 .next_elements 属性对当前tag的之后的tag和字符串进行迭代;
find_all_next() 方法返回所有符合条件的节点;
find_next() 方法返回第一个符合条件的节点。
8 find_all_previous() 和 find_previous()
参数原型:
find_all_previous( name , attrs , recursive , string ,**kwargs )
find_previous( name , attrs , recursive , string ,**kwargs )
这2个方法通过 .previous_elements 属性对当前节点前面的tag和字符串进行迭代;
find_all_previous() 方法返回所有符合条件的节点;
find_previous() 方法返回第一个符合条件的节点。
9 本文涉及的源码
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2023/2/17 # 文件名称:bs04.py# 作用:beautifulsoup的应用# 联系:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonfrom bs4 import BeautifulSoup
html_doc ="""
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc,'html.parser')# ====== 过滤器 ======# 字符串print(soup.find_all('b'))# 正则表达式import re
for tag in soup.find_all(re.compile("^b")):print(tag.name)# 列表print(soup.find_all(["a","b"]))# Truefor tag in soup.find_all(True):print(tag.name)# ====== find_all() ======print(soup.find_all("title"))print(soup.find_all(id='link2'))print(soup.find_all("a", class_="sister"))print(soup.find_all(string="Elsie"))print(soup.find_all("a", limit=2))print(soup.html.find_all("title", recursive=False))# ====== find() ======print(soup.find("nosuchtag"))
a_string = soup.find(string="Lacie")print(a_string)print(a_string.find_parents("a"))
first_link = soup.a
print(first_link)print(first_link.find_next_siblings("a"))
first_story_paragraph = soup.find("p","story")print(first_story_paragraph.find_next_sibling("p"))