当前位置:网站首页>14 BS object Node name Name attrs string get node name attribute content
14 BS object Node name Name attrs string get node name attribute content
2022-06-25 07:23:00 【Andy Python learning notes】
14 bs object . The name of the node .name attrs string Get node name attribute Content
14.1 Extract node name attribute Method of content
tag [tæɡ]: label .
attr: attribute .
string [strɪŋ]: character string .
1. Get node name
Grammar format :bs object . The name of the node .name
Data type returned : character string
from bs4 import BeautifulSoup
html_str = """<p align="center"><strong> It should be green, fat, red and thin .</strong></p>"""
bs_duixiang = BeautifulSoup(html_str,"lxml")
# obtain p Name of node
print(bs_duixiang.p.name)
print(type(bs_duixiang.p.name))
【 Terminal output 】
p
<class 'str'>
Output after running the code p
Is the node name , The data type is string .
2. Get node properties
Grammar format :bs object . The name of the node .attrs
Data type returned : Dictionaries
from bs4 import BeautifulSoup
html_str = """<p align="center"><strong> It should be green, fat, red and thin .</strong></p>"""
bs_duixiang = BeautifulSoup(html_str,"lxml")
# obtain p Properties of a node
print(bs_duixiang.p.attrs)
print(type(bs_duixiang.p.attrs))
【 Terminal output 】
{'align': 'center'}
<class 'dict'>
Output after running the code align': 'center
Attribute for node , The data type is dictionary .
align[əˈlaɪn]: Alignment mode .
center[ˈsentə]: In the middle .
align Represents the code attribute name .
center Represents the property value .
3. Get node content
Grammar format :bs object . The name of the node .string
Data type returned : Traversable string objects .
from bs4 import BeautifulSoup
html_str = """<p align="center"><strong> It should be green, fat, red and thin .</strong></p>"""
bs_duixiang = BeautifulSoup(html_str,"lxml")
# obtain p Content of node
print(bs_duixiang.p.string)
print(type(bs_duixiang.p.string))
【 Terminal output 】
It should be green, fat, red and thin .
<class 'bs4.element.NavigableString'>
14.2 Practice
# Declare a string variable , Storage part HTML Code
html_str = """ <div id="ArtContent"> <h1> Appreciation of classical poems by Li Qingzhao ——《 Like a dream 》</h1> </div> <p align="center"><strong> Last night, it was windy ,</strong></p> <p align="center"><strong> Deep sleep does not eliminate the wine ,</strong></p> <p align="center"><strong> Let's ask the roller shutter ,</strong></p> <p align="center"><strong> But the Begonia is still .</strong></p> <p align="center"><strong> To know whether ,</strong></p> <p align="center"><strong> To know whether ,</strong></p> <p align="center"><strong> It should be green, fat, red and thin .</strong></p> <a href="https://www.diyifanwen.com/m" target="_blank" class="print-link"> """
# step 1: from bs4 Import... In the library BeautifulSoup class
from bs4 import BeautifulSoup
# step 2: Pass in the parameter , Instantiation BeautifulSoup class
# Parameters 1 Is to be parsed HTML character string
# Parameters 2 It's a parser ( Here we use lxml Parser )
# After instantiation, you get a BeautifulSoup object
# bs_duixiang = <class 'bs4.BeautifulSoup'>
bs_duixiang = BeautifulSoup(html_str, 'lxml')
print(" After parsing, the parser gets a BeautifulSoup object :")
print(type(bs_duixiang ),'\n')
# step 3:bs object .tag Name acquisition tag object
print(" The extracted node data type is tag object :")
print(" The first... Is extracted by default p node ")
print(bs_duixiang.p,'\n')
# step 4:bs object . The name of the node .name Extract node label name
print("p The name of the node is :")
print(bs_duixiang.p.name,'\n')
# step 4:bs object . The name of the node .attrs Extract node label attributes
print("p The attribute of the node is :")
print(bs_duixiang.p.attrs,'\n')
# step 4:bs object . The name of the node .string Extract the content of the node label
print("p The content of the node is :")
print(bs_duixiang.p.string,'\n')
print("name The data type of is :",type(bs_duixiang.p.name))
print("attrs The data type of is :",type(bs_duixiang.p.attrs))
print("string The data type of is :",type(bs_duixiang.p.string))
【 Terminal output 】
After parsing, the parser gets a BeautifulSoup object :
<class 'bs4.BeautifulSoup'>
The extracted node data type is tag object :
The first... Is extracted by default p node
<p align="center"><strong> Last night, it was windy ,</strong></p>
p The name of the node is :
p
p The attribute of the node is :
{'align': 'center'}
p The content of the node is :
Last night, it was windy ,
name The data type of is : <class 'str'>
attrs The data type of is : <class 'dict'>
string The data type of is : <class 'bs4.element.NavigableString'>
14.3 summary
边栏推荐
- 有了 MySQL 为什么要用 NoSQL?
- College entrance examination voluntary filling, why is the major the last consideration?
- Jameswebb Space Telescope goes into operation to help study interstellar objects
- Changing the background color of tab bar - changing the background color of tab bar
- 线程状态变化涉及哪些常用 API
- 正版photoshop2022購買體驗經曆分享
- The perfect presentation of Dao in the metauniverse, and platofarm creates a farm themed metauniverse
- How to recover redis data from snapshot(rdb file) copied from another machine?
- Conditional grouping with $exists inside $cond
- Shell命令学习
猜你喜欢
5g private network market is in full swing, and it is crucial to solve deployment difficulties in 2022
Harmony美食菜单界面
韩信大招:一致性哈希
Design of PWM breathing lamp based on FPGA
48 张图 | 手摸手教你微服务的性能监控、压测和调优
14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
1W字|40 图|硬核 ES 实战
Event registration | Apache pulsar x kubesphere online meetup is coming
The significance and proof of weak large number theorem
Escape analysis of 982 golang
随机推荐
Blue Bridge Cup SCM module code (timer) (code + comments)
赚够钱回老家吗
【他字字不提爱,却句句都是爱】
Make fertilizer Safi from crop residues locally to increase yield by 30% and improve soil
Lotus v1.16.0-rc2 Calibration net
活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热来袭
Harmony美食菜单界面
【LeetCode】two num·两数之和
威迈斯新能源冲刺科创板:年营收17亿 应收账款账面价值近4亿
[C language] one dimensional array
100 times larger than the Milky way, Dutch astronomers found mysterious objects in deep space
Event registration | Apache pulsar x kubesphere online meetup is coming
Kubernetes 集群中流量暴露的几种方案
New research shows that human ability to make decisions and process information does not decline until the age of 60
Shandong finds clean energy that can be used by China for 3800 years? You should know the truth first
TEMPEST HDMI泄漏接收 2
Redirect to previous page after login? PHP - Redirecting to previous page after login? PHP
Shell命令学习
Can we use function pointers in go- Can we have function pointers in Go?
基于 KubeSphere 的分级管理实践