Python爬虫(三):BeautifulSoup库

BeautifulSoup 是一个可以从 HTML 或 XML 文件中提取数据的 Python 库,它能够将 HTML 或 XML 转化为可定位的树形结构,并提供了导航、查找、修改功能,它会自动将输入文档转换为 Unicode 编码,输出文档转换为 UTF-8 编码。

BeautifulSoup 支持 Python 标准库中的 HTML 解析器和一些第三方的解析器,默认使用 Python 标准库中的 HTML 解析器,默认解析器效率相对比较低,如果需要解析的数据量比较大或比较频繁,推荐使用更强、更快的 lxml 解析器。

1 安装

1)BeautifulSoup 安装
如果使用 Debain 或 ubuntu 系统,可以通过系统的软件包管理来安装:apt-get install Python-bs4,如果无法使用系统包管理安装,可以使用 pip install beautifulsoup4 来安装。

2)第三方解析器安装
如果需要使用第三方解释器 lxml 或 html5lib,可是使用如下命令进行安装:apt-get install Python-lxml(html5lib)pip install lxml(html5lib)

看一下主要解析器和它们的优缺点:

解析器 使用方法 优势 劣势
Python标准库 BeautifulSoup(markup,
"html.parser")
Python的内置标准库;
执行速度适中;
文档容错能力强。
Python 2.7.3 or 3.2.2)前的版本中文档容错能力差。
lxml HTML 解析器 BeautifulSoup(markup,
"lxml")
速度快;文档容错能力强。 需要安装C语言库。
lxml XML 解析器
BeautifulSoup(markup,
["lxml-xml"])

BeautifulSoup(markup,
"xml")

速度快;唯一支持XML的解析器。 需要安装C语言库
html5lib BeautifulSoup(markup,
"html5lib")
最好的容错性;
以浏览器的方式解析文档;
生成HTML5格式的文档。
速度慢;
不依赖外部扩展。

2 快速上手

将一段文档传入 BeautifulSoup 的构造方法,就能得到一个文档的对象,可以传入一段字符串或一个文件句柄,示例如下:

1)使用字符串
我们以如下一段 HTML 字符串为例:

html = '''
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>BeautifulSoup学习</title>
</head>
<body>
Hello BeautifulSoup
</body>
</html>
'''

使用示例如下:

from bs4 import BeautifulSoup
#使用默认解析器
soup = BeautifulSoup(html,'html.parser')
#使用 lxml 解析器
soup = BeautifulSoup(html,'lxml')

2)本地文件
还以上面那段 HTML 为例,将上面 HTML 字符串放在 index.html 文件中,使用示例如下:

#使用默认解析器
soup = BeautifulSoup(open('index.html'),'html.parser')
#使用 lxml 解析器
soup = BeautifulSoup(open('index.html'),'lxml')

2.1 对象的种类

BeautifulSoup 将 HTML 文档转换成一个树形结构,每个节点都是 Python 对象,所有对象可以归纳为4种:TagNavigableStringBeautifulSoupComment

1)Tag 对象
Tag 对象与 HTML 或 XML 原生文档中的 tag 相同,示例如下:

soup = BeautifulSoup('<title>BeautifulSoup学习</title>','lxml')
tag = soup.title
tp =type(tag)
print(tag)
print(tp)

#输出结果
'''
<title>BeautifulSoup学习</title>
<class 'bs4.element.Tag'>
'''

Tag 有很多方法和属性,这里先看一下它的的两种常用属性:nameattributes

我们可以通过 .name 来获取 tag 的名字,示例如下:

soup = BeautifulSoup('<title>BeautifulSoup学习</title>','lxml')
tag = soup.title
print(tag.name)

#输出结果
#title

我们还可以修改 tag 的 name,示例如下:

tag.name = 'title1'
print(tag)

#输出结果
#<title1>BeautifulSoup学习</title1>

一个 tag 可能有很多个属性,先看一它的 class 属性,其属性的操作方法与字典相同,示例如下:

soup = BeautifulSoup('<title class="tl">BeautifulSoup学习</title>','lxml')
tag = soup.title
cls = tag['class']
print(cls)

#输出结果
#['tl']

我们还可以使用 .attrs 来获取,示例如下:

ats = tag.attrs
print(ats)

#输出结果
#{'class': ['tl']}

tag 的属性可以被添加、修改和删除,示例如下:

#添加 id 属性
tag['id'] = 1

#修改 class 属性
tag['class'] = 'tl1'

#删除 class 属性
del tag['class']

2)NavigableString 对象
NavigableString 类是用来包装 tag 中的字符串内容的,使用 .string 来获取字符串内容,示例如下:

str = tag.string

可以使用 replace_with() 方法将原有字符串内容替换成其它内容 ,示例如下:

tag.string.replace_with('BeautifulSoup')

3)BeautifulSoup 对象
BeautifulSoup 对象表示的是一个文档的全部内容,它并不是真正的 HTML 或 XML 的 tag,因此它没有 nameattribute 属性,为方便查看它的 name 属性,BeautifulSoup 对象包含了一个值为 [document] 的特殊属性 .name,示例如下:

soup = BeautifulSoup('<title class="tl">BeautifulSoup学习</title>','lxml')
print(soup.name)

#输出结果
#[document]

4)Comment 对象
Comment 对象是一个特殊类型的 NavigableString 对象,它会使用特殊的格式输出,看一下例子:

soup = BeautifulSoup('<title class="tl">Hello BeautifulSoup</title>','html.parser')
comment = soup.title.prettify()
print(comment)

#输出结果
'''
<title class="tl">
 Hello BeautifulSoup
</title>
'''

我们前面看的例子中 tag 中的字符串内容都不是注释内容,现在将字符串内容换成注释内容,我们来看一下效果:

soup = BeautifulSoup('<title class="tl"><!--Hello BeautifulSoup--></title>','html.parser')
str = soup.title.string
print(str)

#输出结果
#Hello BeautifulSoup

通过结果我们发现注释符号 <!----> 被自动去除了,这一点我们要注意一下。

2.2 搜索文档树

BeautifulSoup 定义了很多搜索方法,我们来具体看一下。

1)find_all()
find_all() 方法搜索当前 tag 的所有 tag 子节点,方法详细如下:find_all(name=None, attrs={}, recursive=True, text=None,limit=None, **kwargs),来具体看一下各个参数。

name 参数可以查找所有名字为 name 的 tag,字符串对象会被自动忽略掉,示例如下:

soup = BeautifulSoup('<title class="tl">Hello BeautifulSoup</title>','html.parser')
print(soup.find_all('title'))

#输出结果
#[<title class="tl">Hello BeautifulSoup</title>]

attrs 参数定义一个字典参数来搜索包含特殊属性的 tag,示例如下:

soup = BeautifulSoup('<title class="tl">Hello BeautifulSoup</title>','html.parser')
soup.find_all(attrs={"class": "tl"})

调用 find_all() 方法时,默认会检索当前 tag 的所有子孙节点,通过设置参数 recursive=False,可以只搜索 tag 的直接子节点,示例如下:

soup = BeautifulSoup('<html><head><title>Hello BeautifulSoup</title></head></html>','html.parser')
print(soup.find_all('title',recursive=False))

#输出结果
#[]

通过 text 参数可以搜搜文档中的字符串内容,它接受字符串、正则表达式、列表、True,示例如下:

from bs4 import BeautifulSoup
import re

soup = BeautifulSoup('<head>myHead</head><title>BeautifulSoup</title>','html.parser')
#字符串
soup.find_all(text='BeautifulSoup')

#正则表达式
soup.find_all(soup.find_all(text=re.compile('title')))

#列表
soup.find_all(soup.find_all(text=['head','title']))

#True
soup.find_all(text=True)

limit 参数与 SQL 中的 limit 关键字类似,用来限制搜索的数据,示例如下:

soup = BeautifulSoup('<a id="link1" href="http://example.com/elsie">Elsie</a><a id="link2" href="http://example.com/elsie">Elsie</a>','html.parser')
soup.find_all('a', limit=1)

我们经常见到 Python 中 *arg**kwargs 这两种可变参数,*arg 表示非键值对的可变数量的参数,将参数打包为 tuple 传递给函数; **kwargs 表示关键字参数,参数是键值对形式的,将参数打包为 dict 传递给函数。

使用多个指定名字的参数可以同时过滤 tag 的多个属性,如:

soup = BeautifulSoup('<a id="link1" href="http://example.com/elsie">Elsie</a><a id="link2" href="http://example.com/elsie">Elsie</a>','html.parser')
soup.find_all(href=re.compile("elsie"),id='link1')

有些 tag 属性在搜索不能使用,如 HTML5 中的 data-* 属性,示例如下:

soup = BeautifulSoup('<div data-foo="value">foo!</div>')
soup.find_all(data-foo='value')

首先当我在 Pycharm 中输入 data-foo=‘value‘ 便提示语法错误了,然后我不管提示直接执行提示 SyntaxError: keyword can‘t be an expression 这个结果也验证了 data-* 属性在搜索中不能使用。我们可以通过 find_all() 方法的 attrs 参数定义一个字典参数来搜索包含特殊属性的 tag,示例如下:

print(soup.find_all(attrs={'data-foo': 'value'}))

2)find()
方法详细如下:find(name=None, attrs={}, recursive=True, text=None,**kwargs),我们可以看出除了少了 limit 参数,其它参数与方法 find_all 一样,不同之处在于:find_all() 方法的返回结果是一个列表,find() 方法返回的是第一个节点,find_all() 方法没有找到目标是返回空列表,find() 方法找不到目标时,返回 None。来看个例子:

soup = BeautifulSoup('<a id="link1" href="http://example.com/elsie">Elsie</a><a id="link2" href="http://example.com/elsie">Elsie</a>','html.parser')
print(soup.find_all('a', limit=1))
print(soup.find('a'))

#输出结果
'''
[<a href="http://example.com/elsie" id="link1">Elsie</a>]
<a href="http://example.com/elsie" id="link1">Elsie</a>
'''

从示例中我们也可以看出,find() 方法返回的是找到的第一个节点。

3)find_parents() 和 find_parent()
find_all() 和 find() 用来搜索当前节点的所有子节点,find_parents() 和 find_parent() 则用来搜索当前节点的父辈节点。

4)find_next_siblings() 和 find_next_sibling()
这两个方法通过 .next_siblings 属性对当前 tag 所有后面解析的兄弟 tag 节点进行迭代,find_next_siblings() 方法返回所有符合条件的后面的兄弟节点,find_next_sibling() 只返回符合条件的后面的第一个tag节点。

5)find_previous_siblings() 和 find_previous_sibling()
这两个方法通过 .previous_siblings 属性对当前 tag 前面解析的兄弟 tag 节点进行迭代,find_previous_siblings() 方法返回所有符合条件的前面的兄弟节点,find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点。

6)find_all_next() 和 find_next()
这两个方法通过 .next_elements 属性对当前 tag 之后的 tag 和字符串进行迭代,find_all_next() 方法返回所有符合条件的节点,find_next() 方法返回第一个符合条件的节点。

7)find_all_previous() 和 find_previous()
这两个方法通过 .previous_elements 属性对当前节点前面的 tag 和字符串进行迭代,find_all_previous() 方法返回所有符合条件的节点,find_previous() 方法返回第一个符合条件的节点。

2.3 CSS选择器

BeautifulSoup 支持大部分的 CSS 选择器,在 Tag 或 BeautifulSoup 对象的 .select() 方法中传入字符串参数,即可使用 CSS 选择器的语法找到 tag,返回类型为列表。示例如下:

soup = BeautifulSoup('<body><a id="link1" class="elsie">Elsie</a><a id="link2" class="elsie">Elsie</a></body>','html.parser')
print(soup.select('a'))

#输出结果
#[<a clss="elsie" id="link1">Elsie</a>, <a clss="elsie" id="link2">Elsie</a>]

通过标签逐层查找

soup.select('body a')

找到某个 tag 标签下的直接子标签

soup.select('body > a')

通过类名查找

soup.select('.elsie')
soup.select('[class~=elsie]')

通过 id 查找

soup.select('#link1')

使用多个选择器

soup.select('#link1,#link2')

通过属性查找

soup.select('a[class]')

通过属性的值来查找

soup.select('a[class="elsie"]')

查找元素的第一个

soup.select_one('.elsie')

查找兄弟节点标签

#查找所有
soup.select('#link1 ~ .elsie')
#查找第一个
soup.select('#link1 + .elsie')

参考:
https://beautifulsoup.readthedocs.io/zh_CN/latest/#next-siblings-previous-siblings

原文地址:https://www.cnblogs.com/ityard/p/11629589.html

时间: 2024-08-28 18:53:24

Python爬虫(三):BeautifulSoup库的相关文章

Python爬虫的Urllib库有哪些高级用法?

本文和大家分享的主要是python爬虫的Urllib库的高级用法相关内容,一起来看看吧,希望对大家学习python有所帮助. 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它 是一段HTML代码,加 JS.CSS,如果把网页比作一个人,那么HTML便是他的骨架,JS便是他的肌肉,CSS便是它的衣服.所以最重要的部分是存在于HTML中的,下面我 们就写个例子来扒一个网页下来. imp

Python 爬虫—— requests BeautifulSoup

本文记录下用来爬虫主要使用的两个库.第一个是requests,用这个库能很方便的下载网页,不用标准库里面各种urllib:第二个BeautifulSoup用来解析网页,不然自己用正则的话很烦. requests使用,1直接使用库内提供的get.post等函数,在比简单的情况下使用,2利用session,session能保存cookiees信息,方便的自定义request header,可以进行登陆操作. BeautifulSoup使用,先将requests得到的html生成BeautifulSo

Python爬虫之Urllib库的基本使用

Python爬虫之Urllib库的基本使用 import urllib2 response = urllib2.urlopen("http://www.baidu.com") print response.read() 其实上面的urlopen参数可以传入一个request请求,它其实就是一个Request类的实例,构造时需要传入Url,Data等等的内容.比如上面的两行代码,我们可以这么改写 # -*- coding: utf-8 -*- """ Cre

python下载安装BeautifulSoup库

python下载安装BeautifulSoup库 1.下载https://www.crummy.com/software/BeautifulSoup/bs4/download/4.5/ 2.解压到解压到python目录下: 3.“win+R”进入cmd:依次输入如下代码: C:\Users\Administrator>cd D:\softwareIT\Python27\beautifulsoup4-4.5.0 C:\Users\Administrator>d: D:\softwareIT\Py

Python:使用 BeautifulSoup 库抓取百度天气

最近研究了Python的BeautifulSoup库,用起来还挺好玩的一.安装:使用pip命令在线安装:在cmd窗口中输入:pip install beautilfulsoup4 二.代码思路:1.使用request获取相关网页的返回值,即HTML对象: 方法一2.通过BeautifulSoup库对HTML页面元素进行解析,需要先分析要抓取的内容在哪里,再通过代码获取,存储在列表中:方法二3.读取列表中内容,写入到csv文件中.方法三 ```pythonfrom bs4 import Beaut

Python爬虫之Beautifulsoup模块的使用

一 Beautifulsoup模块介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.你可能在寻找 Beautiful Soup3 的文档,Beautiful Soup 3 目前已经停止开发,官网推荐在现在的项目中使用Beautiful Soup 4, 移植到BS4 #安装 Beautiful Soup pip instal

python爬虫(四)_urllib2库的基本使用

本篇我们将开始学习如何进行网页抓取,更多内容请参考:python学习指南 urllib2库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地.在Python中有很多库可以用来抓取网页,我们先学习urllib2. urllib2是Python2.x自带的模块(不需要下载,导入即可使用) urllib2官网文档:https://docs.python.org/2/library/urllib2.html urllib2源码 urllib2在python3.x中被

Python爬虫(三)爬淘宝MM图片

直接上代码: # python2 # -*- coding: utf-8 -*- import urllib2 import re import string import os import shutil def crawl_taobaoMM(baseUrl, start, end): imgDir = 'mm_img' isImgDirExist = os.path.exists(imgDir) if not isImgDirExist: os.makedirs(imgDir) else:

python爬虫基础03-requests库

优雅到骨子里的Requests 本文地址:https://www.jianshu.com/p/678489e022c8 简介 上一篇文章介绍了Python的网络请求库urllib和urllib3的使用方法,那么,作为同样是网络请求库的Requests,相对于urllib,有什么优点呢? 其实,只有两个词,简单优雅. Requests的宣言就是:HTTP for Humans.可以说,Requests彻底贯彻了Python所代表的简单优雅的精神. 之前的urllib做为Python的标准库,因为历

Python爬虫【解析库之beautifulsoup】

解析库的安装 pip3 install beautifulsoup4 初始化 BeautifulSoup(str,"解析库") from bs4 import BeautifulSoup html='''<div class="panel"> <div class="panel-heading"> <h4>Hello</h4> </div> <div class="pan