基于bs4库的HTML内容遍历与输出

bs4库可以对HTML页面进行遍历,分为三种,下行遍历、上行遍历和平行遍历

标签树的下行遍历
属性说明
.contents子节点的列表,将<tag>所有儿子存入列表
.children子节点的迭代类型,与.content类似,用于循环遍历儿子节点
.descendants子孙几点的迭代类型,包含所有的子孙节点,用于循环遍历

 代码如下:

import requests
from bs4 import BeautifulSoup
r = requests.get("https://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo,"html.parser")
soup.head
<head><title>This is a python demo page</title></head>
soup.head.contents
[<title>This is a python demo page</title>]
>>> soup.body.contents
['\n', <p class="title"><b>The demo python introduces several python courses.</b></p>, '\n', <p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:

<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>, '\n']
 len(soup.body.contents)
5
 soup.body.contents[1]
<p class="title"><b>The demo python introduces several python courses.</b></p>
 

通用代码如下:

for child in soup.body.children:
    print(child)  //遍历儿子节点

for child in soup.body.descendants:
    print(child)  //遍历子孙节点

上行便利

标签树的上行遍历
属性说明
.parent节点的父亲标签
.parents节点先辈标签的迭代类型,用于循环遍历先辈节点

代码如下:

>>> soup.title.parent
<head><title>This is a python demo page</title></head>
>>> soup.html.parent
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:

<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
</body></html>
>>> soup.parent
>>> 
>>> soup.parent
>>> soup = BeautifulSoup(demo,"html.parser")
>>> for parent in soup.a.parents:
	if parent is None:
		print(parent)
	else:
		print(parent.name)

p
body
html
[document]

 平行遍历:必须发生在同一父节点下的各节点间

标签树的平行遍历
属性说明
.next.sibling返回按照HTML文本顺序的下一个平行节点标签
.previous.sibling返回按照HTML文本顺序的上一个平行节点标签
.next.siblings迭代类型,返回按照HTML文本顺序的后续所有平行节点标签
.previous.siblings迭代类型,返回按照HTML文本顺序的前续所有平行节点标签

 

>>> soup.a.next_sibling
' and '
>>> soup.a.next_sibling.next_sibling
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>
>>> soup.a.previous_sibling
'Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n'
>>> soup.a.previous_sibling.previous_sibling
>>> soup.a.parent
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:

<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
>>> 

 循环遍历如下:

for sibling in soup.a.next.siblings:
    print(sibling)//遍历后续节点

for sibling in soup.a.previous.siblings:
    print(sibling)//遍历前续节点

 输出方法:

使用prettify函数进行美化,可以为html的文本增加换行符,也可以对某一特定的标签进行处理

bs4将任何读入的内容都转化为utf-8编码

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值