BeautifulSoup 的遍历的实现方法
BeautifulSoup库是对标签树功能的遍历集合
BeautifulSoup 的遍历包括
- 下行遍历
- 上行遍历
- 平行遍历
注意:’and ’,‘\n’也都是节点,在使用 len() 计算节点个数需要计入个数,在平行遍历时也要作为单独节点列出。
下行遍历
属性 | 用法 |
---|---|
.contents | 节点的儿子节点,返回类型是列表 |
.children | 循环遍历儿子节点 |
.descendants | 循环遍历子孙节点 |
.content 用法
例如:
soup.head.contents
类似返回结果:[ < title> this is a page </ title > ]
soup.body.contents
类似返回结果:[ ’ \n '. < p > XXXXX </ p > < p ><a class=“py” href=“http://” </ a > </ p > ]
.children 用法(遍历得到儿子节点)
for n in soup.body.children:
print(n)
.descendants 用法(遍历得到子孙节点)
for n in soup.body.descendants:
print(n)
上行遍历
属性 | 用法 |
---|---|
.parent | 节点的父亲标签 |
.parents | 循环遍历先辈节点 |
.parent 用法
例如:
soup.titleparent
类似返回结果:< head >< title > this is a page < /title >< /head >
.parents用法
soup = BeautifulSoup(demo,“html.parser”)
for n in soup.a.parents:
if n is None:
print(n)
else:
print(n.name)
平行遍历
属性 | 用法 |
---|---|
.next_sibling | 返回按照HTML文本顺序的下一个平行标签 |
.previous_sibling | 返回按照HTML文本顺序的上一个平行标签 |
.next_siblings | 迭代返回按照HTML文本顺序的后面的所有平行标签 |
.previous_siblings | 迭代返回按照HTML文本顺序的前面的所有平行标签 |
注:平行节点是处于同一父节点下的节点
.next_sibling 用法(遍历后续节点)
for n in soup.a.next_sibling:
print(n)
.previous_sibling 用法(遍历前沿节点)
for n in soup.a.previous_sibling:
print(n)