当前位置:网站首页>15 BS object Node name Node name String get nested node content

15 BS object Node name Node name String get nested node content

2022-06-26 14:44:00 Andy Python learning notes

15 bs object . The name of the node . The name of the node .string Get the contents of nested nodes

15.1 Methods to get the contents of nested nodes


 Insert picture description here

Grammar format :bs object . The name of the node . The name of the node .string
Data type returned :<class ‘bs4.element.Tag’> Node object

from bs4 import BeautifulSoup
html_str = """<head><title> To know whether ! To know whether !</head></title>"""
bs_duixiang = BeautifulSoup(html_str,"lxml")

# bs object . The name of the node 
print(" obtain head node :")
print(bs_duixiang.head,'\n')

# bs object . The name of the node . The name of the node 
# head  There are also child nodes in the node title
print(" obtain head Child of a node title:")
print(bs_duixiang.head.title,'\n')

print(" Capture the head Child of a node title The type of :")
print(type(bs_duixiang.head.title),'\n')

# bs object . The name of the node .. The name of the node string Get node content 
print(" Get child nodes title The content of :")
print(bs_duixiang.head.title.string,'\n')

print(" Get child nodes title Data type of content for :")
print(type(bs_duixiang.head.title.string))

【 Terminal output 】

 obtain head node :
<head><title> To know whether ! To know whether !</title></head> 

 obtain head Child of a node title:
<title> To know whether ! To know whether !</title> 

 Capture the head Child of a node title The type of :
<class 'bs4.element.Tag'> 

 Get child nodes title The content of :
 To know whether ! To know whether ! 

 Get child nodes title Data type of content for :
<class 'bs4.element.NavigableString'>

15.2 summary

 Insert picture description here

原网站

版权声明
本文为[Andy Python learning notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261401137233.html