当前位置:网站首页>13 `bs_duixiang.tag标签`得到一个tag对象
13 `bs_duixiang.tag标签`得到一个tag对象
2022-06-25 06:38: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 总结

边栏推荐
- Design a MySQL table for message queue to store message data
- The most basic difference between clustering and classification.
- Is it possible to use Jasmine's toHaveBeenCalledWith matcher with a regular expression?
- How to recover redis data from snapshot(rdb file) copied from another machine?
- Pratique de gestion hiérarchique basée sur kubesphere
- N – simple encoding
- How do I know if mysqlnd is an active driver- How to know if MySQLnd is the active driver?
- 活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
- Shell command learning
- Simple and complete steps of vivado project
猜你喜欢

One year's time and University experience sharing with CSDN

1W字|40 图|硬核 ES 实战

Error reported during vivado simulation common 17-39

Streamnational platform version 1.5 is released, integrating istio and supporting openshift deployment

基于 KubeSphere 的分级管理实践

We are different

5g private network market is in full swing, and it is crucial to solve deployment difficulties in 2022

TEMPEST HDMI泄漏接收 1

终于等到你,小程序开源啦~

破万,我用了六年!
随机推荐
Enter an integer with any number of bits, and output the sum of each bit of the number. For example: 1234 – > 10
How do I get red green blue (RGB) and alpha back from a UIColor object?
lotus v1.16.0-rc2 Calibration-net
Shell命令学习
How is the network connected?
TEMPEST HDMI泄漏接收 1
Keil debug view variable prompt not in scope
From perceptron to transformer, a brief history of deep learning
【LeetCode】two num·两数之和
[Shangshui Shuo series] day 5
shell 上下两行合并成一行
Kube scheduler source code analysis (1) - initialization and startup analysis
哇哦,好丰富呀。
鸿蒙页面菜单的选择
LabVIEW jump to web page
破万,我用了六年!
Want to self-study SCM, do you have any books and boards worth recommending?
ES can finally find brother Wukong!
【C语言】给字符串增加分隔符
【一起上水硕系列】Day 5