当前位置:网站首页>13 `bs_duixiang.tag标签`得到一个tag对象
13 `bs_duixiang.tag标签`得到一个tag对象
2022-06-24 00:34:00 【安迪Python】
13 bs_duixiang.tag标签得到一个tag对象
13.1 BeautifulSoup类提取数据的方法
选择器的作用:操作BeautifulSoup对象,查找、定位元素,并提取数据。

13.2 节点选择器
1. 什么是节点
<h1>一级标题</h1>
<h2>二级标题</h2>
<h3>三级标题</h3>
<p>我是一个段落</p>
上述代码是一段HTML代码。h表示标题标签。标题标签一共有6个,从h1到h6,数字越大,字号越小。p表示段落标签,用于给段落分段,在网页上独占一行。
在HTML代码中的我把hp叫做HTML标签。
在Python中,我们把hp叫做节点标签,即tag标签。
【温馨提示】这样的表述更方便初学者理解节点,和很多官方的教材有出入,仅供参考。
2. 提取节点
语法格式:bs对象.tag名称
tag指节点名称。
返回值:节点对象。
用html.parser解析器提取h2节点
html_str = """
<h2>霸王别姬</h2>
<span>中国内地、中国香港</span>
<span>171分钟</span>
<h3>剧情简介</h3>
<a href="https://maoyan.com/"></a>
"""
# 步骤1:从bs4 库中导入BeautifulSoup类
from bs4 import BeautifulSoup
# 步骤2:传入参数,实例化BeautifulSoup类
# 参数1是要解析的HTML字符串
# 参数2是解析器(这里用html.parser解析器)
# 实例化后得到一个BeautifulSoup对象
# bs_duixiang = <class 'bs4.BeautifulSoup'>
bs_duixiang = BeautifulSoup(html_str, 'html.parser')
print("解析器解析后得到一个BeautifulSoup对象:")
print(type(bs_duixiang ),'\n')
# bs对象.tag名称提取节点
print("这里提取h2节点")
print(bs_duixiang.h2,'\n')
print("提取到的节点数据类型为tag对象:")
print(type(bs_duixiang .h2))
【终端输出】
解析器解析后得到一个BeautifulSoup对象:
<class 'bs4.BeautifulSoup'>
这里提取h2节点
<h2>霸王别姬</h2>
提取到的节点数据类型为tag对象:
<class 'bs4.element.Tag'>
运行代码后,成功输出了h2节点。
用lxml解析器提取span节点
html_str = """
<h2>霸王别姬</h2>
<span>中国内地、中国香港</span>
<span>171分钟</span>
<h3>剧情简介</h3>
<a href="https://maoyan.com/"></a>
"""
# 步骤1:从bs4 库中导入BeautifulSoup类
from bs4 import BeautifulSoup
# 步骤2:传入参数,实例化BeautifulSoup类
# 参数1是要解析的HTML字符串
# 参数2是解析器(这里用lxml解析器)
# 实例化后得到一个BeautifulSoup对象
# bs_duixiang = <class 'bs4.BeautifulSoup'>
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print("解析器解析后得到一个BeautifulSoup对象:")
print(type(bs_duixiang ),'\n')
# bs对象.tag名称提取节点
print("这里提取span节点")
print(bs_duixiang.span,'\n')
print("提取到的节点数据类型为tag对象:")
print(type(bs_duixiang .span))
【终端输出】
解析器解析后得到一个BeautifulSoup对象:
<class 'bs4.BeautifulSoup'>
这里提取span节点
<span>中国内地、中国香港</span>
提取到的节点数据类型为tag对象:
<class 'bs4.element.Tag'>
上述html_str字符中有2个标签。
代码运行成功后,我们提取到了中国内地、中国香港标签。
即2个标签中的第1个。
那是因为当html代码中存在多个相同的节点,节点选择器只会提取第1个节点。
13.3 总结

边栏推荐
- [image detection saliency map] calculation of fish eye saliency map based on MATLAB distortion prompt [including Matlab source code 1903]
- Shuttle global levitation button
- 钟珊珊:被爆锤后的工程师会起飞|OneFlow U
- An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
- paddle使用指南
- C语言:结构体数组实现找出最低分学生记录
- WinSCP和PuTTY的安装和使用
- C语言:利用自定义函数排序
- 阿里巴巴面试题:多线程相关
- C language: sorting with custom functions
猜你喜欢

实时计算框架:Spark集群搭建与入门案例

LSF打开Job idle information以看job的cpu time/elapse time使用情况

C language: sorting with custom functions

Niu Xuechang's anniversary celebration: software promotion, limited time and free registration code!

苹果Iphone14搭载北斗导航系统,北斗VS GPS有哪些优势?

毕业设计-论文写作笔记【毕设题目类型、论文写作细节、毕设资料】

Tiktok practice ~ one click registration and login process of mobile phone number and password (restrict mobile terminal login)

阿里巴巴面试题:多线程相关

How to get started with machine learning?

【虹科案例】3D数据如何成为可操作的信息?– 对象检测和跟踪
随机推荐
Android 72021 latest Android interview written test questions sharing
ShardingSphere-proxy-5.0.0容量范围分片的实现(五)
Shutter time selector
解决base64压缩文件,经过post请求解压出来是空格的问题
[CVPR 2020] conference version: a physics based noise formation model for extreme low light raw denoising
Nlp-d58-nlp competition d27 & question brushing D14 & Paper Reading & MathType
C语言:百马百担问题求驮法
Chinese guide to accompanist component library - glide, hot
UART protocol timing summary
【小程序】相对路径和绝对路径的表示符
应用配置管理,基础原理分析
Empty encoded password警告原因
If you want to open an account for stock trading, is it safe to open an account online-
Security | warm tips: security incidents on the cloud have intensified recently. Please do a good job in backup monitoring of cloud security protection!
Android - JNI 开发你所需要知道的基础,Android工程师面试题
[digital signal] spectrum refinement based on MATLAB analog window function [including Matlab source code 1906]
Building a digital software factory -- panoramic interpretation of one-stop Devops platform
[CVPR 2022] high resolution small object detection: cascaded sparse query for accelerating high resolution smal object detection
[image detection saliency map] calculation of fish eye saliency map based on MATLAB distortion prompt [including Matlab source code 1903]
version `ZLIB_1.2.9‘ not found (required by /lib64/libpng16.so.16)