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

边栏推荐
- The significance and proof of weak large number theorem
- TEMPEST HDMI泄漏接收 1
- Americo technology launches professional desktop video editing solution
- Streamnational platform version 1.5 is released, integrating istio and supporting openshift deployment
- Blue Bridge Cup SCM module code (matrix key) (code + comments)
- lotus v1.16.0-rc2 Calibration-net
- How do I create a guid in excel- How to create a GUID in Excel?
- Debug through yalc before releasing NPM package
- Practice of hierarchical management based on kubesphere
- 14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
猜你喜欢

Cocos学习日记3——api获取节点、组件

Ppt template of small fresh open class education courseware

为什么要“除夕”,原来是内存爆了!

Operate cnblogs metaweblog API

破万,我用了六年!

Practice of hierarchical management based on kubesphere

13 `bs_duixiang.tag标签`得到一个tag对象

网络是怎样连接的?
![[Introduction aux uvm== > Episode 9] ~ modèle de registre, intégration du modèle de registre, méthode conventionnelle du modèle de registre, scénario d'application du modèle de registre](/img/c0/b373a3f0e0c7b35f42c8a28b4d4f74.png)
[Introduction aux uvm== > Episode 9] ~ modèle de registre, intégration du modèle de registre, méthode conventionnelle du modèle de registre, scénario d'application du modèle de registre

Display purchase Summary - Dell 2705qm BenQ pd2700u
随机推荐
线程状态变化涉及哪些常用 API
【UVM入門 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常規方法、寄存器模型的應用場景
Don't you know the evolution process and principle of such a comprehensive redis cluster model?
Too beautiful promise because too young
Is it possible to use Jasmine's toHaveBeenCalledWith matcher with a regular expression?
【xxl-job】池塘水绿风微暖,记得玉真初见面
活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
lotus windowPoSt 手动触发时空证明计算
正版photoshop2022購買體驗經曆分享
Shell command learning
Finally, when you open source the applet ~
lotus v1.16.0-rc2 Calibration-net
Omni toolbox direct download
鸿蒙页面菜单的选择
关于硬件问题造成的MCU死机,过来人简单的谈一谈
SQL query, if value is null then return 1 - SQL query, if value is null then return 1
Using awk to process input from multiple files
lotus v1.16.0-rc3 calibnet
赚够钱回老家吗
Capable people never complain about the environment!