Python中中BeautifuSoup库的用法使用详解库的用法使用详解
主要介绍了Python中BeautifuSoup库的用法使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学
习学习吧
BeautifulSoup简介简介
Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。官方解释如下:
Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个
完整的应用程序。
Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,Beautiful Soup就不能自动识别编码方式了。然后,你
仅仅需要说明一下原始编码方式就可以了。
Beautiful Soup已成为和lxml、html6lib一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度。
BeautifulSoup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python 会使用 Python默认的解析器,lxml 解析器更加强大,速度更快,推荐使用lxml 解析器。
1. 创建创建 BeautifulSoup 对象对象
首先导入库 bs4 lxml requests
#encoding:UTF-8
from bs4 import BeautifulSoup
import lxml
import requests
使用官方字符串来演示:
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse">The Dormouse's story</p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
创建 beautifulsoup 对象:
soup = BeautifulSoup(html,'lxml') #创建 beautifulsoup 对象
还可以用本地 HTML 文件来创建对象:
soup1 = BeautifulSoup(open('index.html')) #用本地 HTML 文件来创建对象
打印一下 soup 对象的内容,格式化输出:
print soup.prettify() #打印 soup 对象的内容,格式化输出
输出结果,格式化打印出了它的内容,这个函数经常用到。
2. 四种对象四种对象
Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:
Tag
NavigableString
BeautifulSoup
Comment
((1))Tag
Tag就是 HTML 中的一个个标签,例如:
<title>The Dormouse's story</title>
用 BeautifulSoup 可以很方便地获取 Tags:
print soup.title
print soup.head
print soup.a
print soup.p
print type(soup.a)
这种方式查找的是在所有内容中的第一个符合要求的标签。
对于 Tag,它有两个重要的属性,name 和 attrs :
print soup.name
print soup.a.name
print soup.attrs
print soup.p.attrs #在这里,我们把 p 标签的所有属性打印输出了出来,得到的类型是一个字典。
print soup.p['class'] #单独获取某个属性
print soup.p.get('class') ##单独获取某个属性 跟上面一样的
可以对这些属性和内容等等进行修改:
soup.p['class']="newClass"
也可以对这个属性进行删除:
del soup.p['class']
(2)NavigableString
得到了标签的内容用 .string 即可获取标签内部的文字,例如:
print soup.p.string
来检查一下它的类型:
print type(soup.p.string)
#<class 'bs4.element.NavigableString'>
可以看到它的类型是一个 NavigableString,翻译过来叫 可以遍历的字符串。
(3)BeautifulSoup
BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称:
print type(soup.name)
#<type 'unicode'>
print soup.name
# [document]
print soup.attrs
#{} 空字典