BeautifulSoup学习笔记2——父节点,兄弟节点

BeautifulSoup学习笔记2——父节点,兄弟节点

1. .parent 返回该节点的父节点,如果不存在则返回None

soup.parent
#None
soup.html
"""
The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

...

"""
soup.html.parent.name #'[document]' soup.p.b #The Dormouse's story soup.p.b.string.parent #The Dormouse's story
  1. .parents返回该节点的所有父节点,从里到外,从上往下
list(soup.p.b.string.parents)
"""
[The Dormouse's story,
 

The Dormouse's story

,

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

...

, The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

...

, The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

...

] """
for i in list(soup.p.b.string.parents): if i: print(i.name) else: break """ b p body html [document] """

.

你可能感兴趣的:(爬虫,数据挖掘)