当前位置:网站首页>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 总结

边栏推荐
- [ICPR 2021] tiny object detection in aerial images
- Android 3年外包工面试笔记,有机会还是要去大厂学习提升,android开发实习面试题
- 【小程序】编译预览小程序时,出现-80063错误提示
- Dart series: using generators in dart
- Vulnerability recurrence - redis vulnerability summary
- SQL数据库:知识点汇总,期末不挂科
- [image detection saliency map] calculation of fish eye saliency map based on MATLAB distortion prompt [including Matlab source code 1903]
- Complete collection of development environment configuration -- Visual Studio 2022 installation
- JS language precision problem
- Summary of common register bit operation modes in MCU
猜你喜欢

Mip-NeRF:抗混叠的多尺度神经辐射场ICCV2021

智能制造时代下,MES管理系统需要解决哪些问题

Skywalking installation and deployment practice

Superscalar processor design yaoyongbin Chapter 3 virtual memory -- Excerpt from subsection 3.1~3.2
![[traffic light identification] traffic light identification based on Matlab GUI [including Matlab source code 1908]](/img/0e/3103c4c5dd664196c85db9b30bcaf5.png)
[traffic light identification] traffic light identification based on Matlab GUI [including Matlab source code 1908]
![[applet] realize the effect of double column commodities](/img/e3/b72955c1ae67ec124520ca46c22773.png)
[applet] realize the effect of double column commodities

Interview notes for Android outsourcing workers for 3 years. You still need to go to a large factory to learn and improve when you have the opportunity. Interview questions for Android Development Int

Social recruitment interview is indispensable -- 1000 interview questions for Android engineers from Internet companies

C语言:关于矩阵右移问题

985本3Android程序员40天拿下阿里P6口头offer,面试成功后整理了这些面试思路
随机推荐
规律/原理/规则/法则/定理/公理/本质/定律
【CVPR 2020 Oral】极低光去噪论文:A Physics-based Noise Formation Model for Extreme Low-light Raw Denoising
Andorid 开发艺术探索笔记(2),跨平台小程序开发框架
产业互联网时代将依靠源自于产业本身的产品、技术和模式来实现的
C语言:结构体数组实现找出最低分学生记录
【ICCV Workshop 2021】基于密度图的小目标检测:Coarse-grained Density Map Guided Object Detection in Aerial Images
Using anydesk remote control for intranet penetration horizontal movement
version `ZLIB_ 1.2.9‘ not found (required by /lib64/libpng16.so.16)
version `ZLIB_1.2.9‘ not found (required by /lib64/libpng16.so.16)
【小程序】相对路径和绝对路径的表示符
Pure JS implementation determines whether the IP is pinged
How many of the 36 difficult points of activity do you know?, Android interview 2020
C语言:百马百担问题求驮法
小猫爪:PMSM之FOC控制15-MRAS法
C language: recursively implementing factorial of n
Empty encoded password警告原因
skywalking 安装部署实践
[technique of planting grass] spit blood and clean up, and take you to collect goose feathers in a fancy way! Do not spread!!!
DML operation
牛学长周年庆活动:软件大促限时抢,注册码免费送!