1. 学习视频
https://www.bilibili.com/video/BV1pt41137qK?p=28
2. <>.find_all(name, attrs, recursive, string, **kwargs)
- name: 对标签名称的检索字符串
- attrs:对标签属性值得检索字符串,可标注属性检索
- recursive: 是否对子孙全部检索,默认True
- string:对标签之间<>…</>中字符串区域的检索字符串
缩写写法:
- (…) 等价于 .find_all(…)
- soup(…) 等价于 soup.find_all(…)
3. 示例代码
import requests
from bs4 import BeautifulSoup
import re
"""
基于 bs4 库的 HTML 内容查找-find_all 的使用
https://www.bilibili.com/video/BV1pt41137qK?p=28
<>.find_all(name, attrs, recursive, string, **kwargs)
name: 对标签名称的检索字符串
attrs:对标签属性值得检索字符串,可标注属性检索
recursive: 是否对子孙全部检索,默认True
string:对标签之间<>..</>中字符串区域的检索字符串
缩写写法:
<tag>(..) 等价于 <tag>.find_all(..)
soup(..) 等价于 soup.find_all(..)
"""
if __name__ == '__main__':
r = requests.get("https://python123.io/ws/demo.html")
soup = BeautifulSoup(r.text, "html.parser")
print(soup.prettify())
print('打印a标签: \n', soup.find_all('a'))
print('---------------------------')
print('打印a和b的标签: \n', soup.find_all(['a','b']))
print('---------------------------')
print('打印出b开头的标签')
for tag in soup.find_all(re.compile("b")):
print(tag.name)
print('---------------------------')
print('打印带有属性值course的p标签 \n', soup.find_all('p', 'course'))
print('---------------------------')
print('打印指定字符串,id="link1')
kv = {'id': 'link1'}
print(soup.find_all(**kv))
print('---------------------------')
# 是否对子孙节点进行搜索
kv = {'recursive': 'False'}
print('打印a标签,但是不对子孙节点进行搜索: \n', soup.find_all('a', **kv))
print('---------------------------')
kv = {'string': 'Basic Python'}
print('打印标签直接的字符串,需要准确匹配: \n', soup.find_all(**kv))
print('---------------------------')```
4. 运行结果
C:\Users\珞落\AppData\Local\Programs\Python\Python39\python.exe D:/PythonProject/main.py
<html>
<head>
<title>
This is a python demo page
</title>
</head>
<body>
<p class="title">
<b>
The demo python introduces several python courses.
</b>
</p>
<p class="course">
Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
Basic Python
</a>
and
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">
Advanced Python
</a>
.
</p>
</body>
</html>
打印a标签:
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
---------------------------
打印a和b的标签:
[<b>The demo python introduces several python courses.</b>, <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
---------------------------
打印出b开头的标签
body
b
---------------------------
打印带有属性值course的p标签
[<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>]
---------------------------
打印指定字符串,id="link1
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>]
---------------------------
打印a标签,但是不对子孙节点进行搜索:
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
---------------------------
打印标签直接的字符串,需要准确匹配:
['Basic Python']
---------------------------
Process finished with exit code 0
|