Python网络爬虫与信息提取(三):使用BeautifulSoup信息提取

1.BeautifulSoup的官网与安装

官方网站

通过pip安装

pip install beautifulsoup4

2.小案例:BeautifulSoup库安装小测

import requests
from bs4 import BeautifulSoup

url = 'https://python123.io/ws/demo.html'
r = requests.get(url)
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
print(soup.prettify())

3.BeautifulSoup库的基本元素

BeautifulSoup库是解析、遍历、维护“标签树”的功能库。
HTML代码=标签树 = BeautifulSoup类

Beautiful Soup库的引用

Beautiful Soup库,也叫beautifulsoup4或bs4。

from bs4 import BeautifulSoup #从bs4库中引入一个BeautifulSoup类型

或者

import bs4

BeautifulSoup类

HTML代码 = 标签树 = BeautifulSoup类
在这里插入图片描述

from bs4 import BeautifulSoup

soup = BeautifulSoup('<html>data</html>', 'html.parser')
soup2 = BeautifulSoup('D://demo.html', 'html.parser')

BeautifulSoup对应一个 HTML / XML 文档全部内容。

BeautifulSoup库解析器

解析器使用方法条件
bs4的HTML解析器BeautifulSoup(mk, 'html.parser')安装bs4库
lxml的HTML解析器BeautifulSoup(mk, 'lxml')pip install lxml
lxml的XML解析器BeautifulSoup(mk, 'xml')pip install lxml
html5lib解析器BeautifulSoup(mk, 'html5lib')pip install html5lib

BeautifulSoup类的基本元素

基本元素说明
Tag标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾
Name标签的名字,<p>…</p>的名字是‘p’,格式:<tag>.name
Attributes标签的属性,字典形式组织,格式:<tag>.attrs
NavigableString标签内非属性字符串,<>…</>中字符串,格式:<tag>.string,能构成节点
Comment标签内字符串的注释部分,一种特殊的Comment类型

BeautifulSoup库的理解

在这里插入图片描述

4.基于bs4库的HTML内容遍历方法

案例HTML页面:

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

HTML标签结构
在这里插入图片描述

标签树的下行遍历

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

遍历儿子节点

for child in soup.body.children:
    print(child)

遍历子孙节点

for child in soup.body.descendants:
    print(child)

标签树的上行遍历

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

遍历a标签的所有先辈标签

import requests
from bs4 import BeautifulSoup

url = 'https://python123.io/ws/demo.html'
r = requests.get(url)
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
for parent in soup.a.parents:
    print(parent.name)

结果:

p
body
html
[document]

标签树的平行遍历

属性说明
.next_sibling返回按照HTML文本顺序的下一个平行节点标签
.previous_sibling返回按照HTML文本顺序的上一个平行节点标签
.next_siblings迭代类型,返回按照HTML文本顺序的后续所有平行节点标签
.previous_siblings迭代类型,返回按照HTML文本顺序的前续所有平行节点标签

要求:平行遍历发生在同一个父节点下的各节点间

遍历后续节点

for sibling in soup.a.next_siblings:
    print(sibling)

遍历前续节点

for sibling in soup.a.previous_siblings:
    print(sibling)

在这里插入图片描述

5.基于bs4库的HTML格式输出

soup.prettify()

from bs4 import BeautifulSoup

soup = BeautifulSoup("<p>中文</p>", "html.parser")
print(soup.p)
print(soup.p.prettify())

输出:

<p>中文</p>
<p>
 中文
</p>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值