学习笔记:基于 bs4 库的 HTML 内容查找-find_all 的使用

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值