当前位置:网站首页>15 bs对象.节点名称.节点名称.string 获取嵌套节点内容
15 bs对象.节点名称.节点名称.string 获取嵌套节点内容
2022-06-26 14:01:00 【安迪python学习笔记】
15 bs对象.节点名称.节点名称.string
获取嵌套节点内容
15.1 获取嵌套节点内容的方法

语法格式:bs对象.节点名称.节点名称.string
返回的数据类型:<class ‘bs4.element.Tag’> 节点对象
from bs4 import BeautifulSoup
html_str = """<head><title>知否!知否!</head></title>"""
bs_duixiang = BeautifulSoup(html_str,"lxml")
# bs对象.节点名称
print("获取head节点:")
print(bs_duixiang.head,'\n')
# bs对象.节点名称.节点名称
# head 节点里还有有个子节点title
print("获取head节点的子节点title:")
print(bs_duixiang.head.title,'\n')
print("获取获取head节点的子节点title的类型:")
print(type(bs_duixiang.head.title),'\n')
# bs对象.节点名称..节点名称string获取节点内容
print("获取子节点title的内容:")
print(bs_duixiang.head.title.string,'\n')
print("获取子节点title的内容的数据类型:")
print(type(bs_duixiang.head.title.string))
【终端输出】
获取head节点:
<head><title>知否!知否!</title></head>
获取head节点的子节点title:
<title>知否!知否!</title>
获取获取head节点的子节点title的类型:
<class 'bs4.element.Tag'>
获取子节点title的内容:
知否!知否!
获取子节点title的内容的数据类型:
<class 'bs4.element.NavigableString'>
15.2 总结
边栏推荐
猜你喜欢
Practice with the topic of bit operation force deduction
Jianzhi offer 43.47.46.48 dynamic planning (medium)
STM32F1和GD32F1有什么区别?
Complimentary Book Cognitive Control: how does our brain accomplish tasks?
在云服务器中云磁盘如何挂载
详解C语言编程题:任意三条边能否构成三角形,输出该三角形面积并判断其类型
The engine "node" is inconsistent with this module
Related knowledge of libsvm support vector machine
MySQL主从复制与读写分离
Mathematical modeling of war preparation 30 regression analysis 2
随机推荐
Jianzhi offer 43.47.46.48 dynamic planning (medium)
详解C语言编程题:任意三条边能否构成三角形,输出该三角形面积并判断其类型
GDAL multiband synthesis tool
工作上对金额价格类小数点的总结以及坑
Sword finger offer 06.24.35 Linked list
Matlab programming related knowledge
Mathematical modeling of war preparation 30 regression analysis 2
Installation tutorial about origin2019
印尼投资部长:鸿海考虑在其新首都建立电动公交系统、城市物联网
'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
ArcGIS secondary development method - layer related operations (add, modify)
D - Face Produces Unhappiness
Sword finger offer 05.58 Ⅱ string
C | analysis of malloc implementation
NAACL2022:(代码实践)好的视觉引导促进更好的特征提取,多模态命名实体识别(附源代码下载)...
Electron
Leaflet loading ArcGIS for server map layers
STM32F1和GD32F1有什么区别?
2022年最新贵州建筑八大员(机械员)模拟考试题库及答案
Deploy the flask environment using the pagoda panel