Beautiful Soup用法

Introduction

Beautiful Soup是一个解析网页和构造结构化数据表达形式的优秀函数库。它允许我们利用类型(type)、ID,或者任何其他的属性来访问网页内的任何元素,并获取到代表其内容的字符串。Beautiful Soup还可以很好地处理包含不规范HTML标记的Web页面,当我们根据站点的内容来构造数据集时,这一点是非常有用的。

Download

我们可以从 http://crummy.com/software/BeautifulSoup 下载到Beautiful Soup。这是一个单独的Python文件,我们可以将它放到Python库所在路径下,或者放到工作路径——即启动Python解释器的路径下。

Usage

待Beautiful Soup安装完毕之后,我们就可以在Python解释器中使用它了:

from BeautifulSoup import BeautifulSoup
from urllib import urlopen
soup = BeautifulSoup(urlopen('http://google.com'))
soup.head.title
<title>Google</title>
links = soup('a')
len(links)
21
links[0]
<a href="http://www.google.com/ig?hl=en">iGoogle</a>
liks[0].contents[0]
u'iGoogle' 

在上一示例中,我们对Google首页的HTML文本进行了解析,并示范了从DOM树上提取元素和查找链接的方法。

import urllib2
from BeautifulSoup import BeautifulSoup
c = urllib2.urlopen('http://kiwitobes.com/wiki/Programming_language.html')
soup = BeautifulSoup(c.read())
links = soup('a')
links[10]
<a href="/wiki/Algorithm.html" title="Algorithm">algorithms</a>
links[10]['href']
u'/wiki/Algorithm.html'

要构造一个soup对象——这是Beautiful Soup描述Web网页的方式——只需利用页面内容对其进行初始化即可。我们可以将标签内容作为参数来调用soup,比如上例中的“a”,调用的结果将返回一个属于该标签类型的对象列表。其中的每一个对象也都是可访问的(addressable),我们可以逐层深入地访问到对象的属性,以及其下所属的其他对象。


Introduction

Beautiful Soup是Python的一个库,最主要的功能是从网页抓取数据。

  • Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序。
  • Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,Beautiful Soup就不能自动识别编码方式了。然后,你仅仅需要说明一下原始编码方式就可以了。
  • Beautiful Soup已成为和lxml、html6lib一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度。

创建Beautiful Soup对象

首先必须要导入bs4库

from bs4 import BeautifulSoup

我们创建一个字符串,后面的例子我们便会用它来演示

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

创建BeautifulSoup对象

soup = BeautifulSoup(html)

另外,我们还可以用本地HTML文件来创建对象,例如

soup = BeautifulSoup(open('index.html'))

上面这句代码便是将本地 index.html 文件打开,用它来创建 soup 对象。

下面我们来打印一下 soup 对象的内容,格式化输出

print soup.prettify()
<html>
 <head>
  <title>
   The Dormouse's story
  </title>
 </head>
 <body>
  <p class="title" name="dromouse">
   <b>
    The Dormouse's story
   </b>
  </p>
  <p class="story">
   Once upon a time there were three little sisters; and their names were
   <a class="sister" href="http://example.com/elsie" id="link1">
    <!-- Elsie -->
   </a>
   ,
   <a class="sister" href="http://example.com/lacie" id="link2">
    Lacie
   </a>
   and
   <a class="sister" href="http://example.com/tillie" id="link3">
    Tillie
   </a>
   ;
and they lived at the bottom of a well.
  </p>
  <p class="story">
   ...
  </p>
 </body>
</html>

以上便是输出结果,格式化打印出了它的内容,这个函数经常用到。

四大对象种类

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

  • Tag
  • NavigableString
  • BeautifulSoup
  • Comment

Tag

Tag 是什么?通俗点讲就是 HTML 中的一个个标签,例如

<title>The Dormouse's story</title>

<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

上面的 title a 等等 HTML 标签加上里面包括的内容就是 Tag,下面我们来感受一下怎样用 Beautiful Soup 来方便地获取 Tags。

下面每一段代码中注释部分即为运行结果

print soup.title
#<title>The Dormouse's story</title>

print soup.head
#<head><title>The Dormouse's story</title></head>

print soup.a
#<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>

print soup.p
#<p class="title" name="dromouse"><b>The Dormouse's story</b></p>

我们可以利用 soup加标签名轻松地获取这些标签的内容,是不是感觉比正则表达式方便多了?不过有一点是,它查找的是在所有内容中的第一个符合要求的标签,如果要查询所有的标签,我们在后面进行介绍。

我们可以验证一下这些对象的类型

print type(soup.a)
#<class 'bs4.element.Tag'>

对于 Tag,它有两个重要的属性,是 name 和 attrs,下面我们分别来感受一下

  • name

    print soup.name
    print soup.head.name
    #[document]
    #head
    

    soup 对象本身比较特殊,它的 name 即为 [document],对于其他内部标签,输出的值便为标签本身的名称。

  • attrs

print soup.p.attrs
#{'class': ['title'], 'name': 'dromouse'}

在这里,我们把 p 标签的所有属性打印输出了出来,得到的类型是一个字典。

如果我们想要单独获取某个属性,可以这样,例如我们获取它的 class 叫什么

print soup.p['class']
#['title']

还可以这样,利用get方法,传入属性的名称,二者是等价的

print soup.p.get('class')
#['title']

我们可以对这些属性和内容等等进行修改,例如

soup.p['class']="newClass"
print soup.p
#<p class="newClass" name="dromouse"><b>The Dormouse's story</b></p>

还可以对这个属性进行删除,例如

del soup.p['class']
print soup.p
#<p name="dromouse"><b>The Dormouse's story</b></p>

不过,对于修改删除的操作,不是我们的主要用途,在此不做详细介绍了,如果有需要,请查看前面提供的官方文档。

NavigableString

既然我们已经得到了标签的内容,那么问题来了,我们要想获取标签内部的文字怎么办呢?很简单,用 .string 即可,例如

print soup.p.string
#The Dormouse's story

这样我们就轻松获取到了标签里面的内容,想想如果用正则表达式要多麻烦。它的类型是一个 NavigableString,翻译过来叫可以遍历的字符串,不过我们最好还是称它英文名字吧。

来检查一下它的类型

print type(soup.p.string)
#<class 'bs4.element.NavigableString'>

BeautifulSoup

BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称,以及属性来感受一下。

print type(soup.name)
#<type 'unicode'>
print soup.name 
# [document]
print soup.attrs 
#{} 空字典

Comment

Comment 对象是一个特殊类型的 NavigableString 对象,其实输出的内容仍然不包括注释符号,但是如果不好好处理它,可能会对我们的文本处理造成意想不到的麻烦。

我们找一个带注释的标签

print soup.a
print soup.a.string
print type(soup.a.string)

运行结果如下

<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>
 Elsie 
<class 'bs4.element.Comment'>

a 标签里的内容实际上是注释,但是如果我们利用 .string 来输出它的内容,我们发现它已经把注释符号去掉了,所以这可能会给我们带来不必要的麻烦。

另外我们打印输出下它的类型,发现它是一个 Comment 类型,所以,我们在使用前最好做一下判断,判断代码如下

if type(soup.a.string)==bs4.element.Comment:
    print soup.a.string

上面的代码中,我们首先判断了它的类型,是否为 Comment 类型,然后再进行其他操作,如打印输出。

遍历文档树

直接子节点

  • .contents
    tag 的 .content 属性可以将tag的子节点以列表的方式输出

    print soup.head.contents 
    #[<title>The Dormouse's story</title>]
    

    输出方式为列表,我们可以用列表索引来获取它的某一个元素

    print soup.head.contents[0]
    #<title>The Dormouse's story</title>
    
  • .children
    它返回的不是一个 list,不过我们可以通过遍历获取所有子节点。
    我们打印输出 .children 看一下,可以发现它是一个 list 生成器对象

    print soup.head.children
    #<listiterator object at 0x7f71457f5710>
    

    我们怎样获得里面的内容呢?很简单,遍历一下就好了,代码及结果如下

    for child in  soup.body.children:
    print child
    
    <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
    
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>,
    <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
    <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>
    
    <p class="story">...</p>
    

所有子孙节点

  • .descendants
    .contents 和 .children 属性仅包含tag的直接子节点,.descendants 属性可以对所有tag的子孙节点进行递归循环,和 children类似,我们也需要遍历获取其中的内容。
    for child in soup.descendants:
    print child

运行结果如下,可以发现,所有的节点都被打印出来了,先是最外层的 HTML标签,其次从 head 标签一个个剥离,以此类推。

    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>,
    <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
    <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>
    <p class="story">...</p>
    </body></html>
    <head><title>The Dormouse's story</title></head>
    <title>The Dormouse's story</title>
    The Dormouse's story


    <body>
    <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>,
    <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
    <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>
    <p class="story">...</p>
    </body>


    <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
    <b>The Dormouse's story</b>
    The Dormouse's story


    <p class="story">Once upon a time there were three little sisters; and their names were
    <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>,
    <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
    <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>
    Once upon a time there were three little sisters; and their names were

    <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>
     Elsie 
    ,

    <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
    Lacie
    and

    <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
    Tillie
    ;
    and they lived at the bottom of a well.


    <p class="story">...</p>
    ...

节点内容

如果tag只有一个 NavigableString 类型子节点,那么这个tag可以使用 .string 得到子节点。如果一个tag仅有一个子节点,那么这个tag也可以使用 .string 方法,输出结果与当前唯一子节点的 .string 结果相同。

通俗点说就是:如果一个标签里面没有标签了,那么 .string 就会返回标签里面的内容。如果标签里面只有唯一的一个标签了,那么 .string 也会返回最里面的内容。例如

print soup.head.string
#The Dormouse's story
print soup.title.string
#The Dormouse's story

如果tag包含了多个子节点,tag就无法确定,string 方法应该调用哪个子节点的内容, .string 的输出结果是 None。

print soup.html.string
# None

多个内容

  • .strings
  • .stripped_strings
    输出的字符串中可能包含了很多空格或空行,使用 .stripped_strings 可以去除多余空白内容

父节点

  • .parent属性

全部父节点

  • .parents属性
    通过元素的 .parents 属性可以递归得到元素的所有父辈节点

兄弟节点

  • .next_sibling属性
  • .previous_sibling属性

兄弟节点可以理解为和本节点处在统一级的节点,.next_sibling 属性获取了该节点的下一个兄弟节点,.previous_sibling 则与之相反,如果节点不存在,则返回 None。

注意:实际文档中的tag的 .next_sibling 和 .previous_sibling 属性通常是字符串或空白,因为空白或者换行也可以被视作一个节点,所以得到的结果可能是空白或者换行。

全部兄弟节点

  • .next_siblings属性
  • .previous_siblings属性

通过 .next_siblings 和 .previous_siblings 属性可以对当前节点的兄弟节点迭代输出

前后节点

  • .next_element属性
  • .previous_element属性

与 .next_sibling .previous_sibling 不同,它并不是针对于兄弟节点,而是在所有节点,不分层次

前后所有节点

  • .next_elements属性
  • .previous_elements属性

通过 .next_elements 和 .previous_elements 的迭代器就可以向前或向后访问文档的解析内容,就好像文档正在被解析一样。

搜索文档树

find_all(name, attrs, recursive, text, **kwargs)

find_all() 方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件。

  1. name参数
    name 参数可以查找所有名字为 name 的tag,字符串对象会被自动忽略掉。
    • 传字符串
      最简单的过滤器是字符串.在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容,下面的例子用于查找文档中所有的<b>标签。
    • 传正则表达式
      如果传入正则表达式作为参数,Beautiful Soup会通过正则表达式的 match() 来匹配内容.下面例子中找出所有以b开头的标签,这表示<body>和<b>标签都应该被找到。
    • 传列表
      如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有<a>标签和<b>标签。
    • 传True
      True 可以匹配任何值,下面代码查找到所有的tag,但是不会返回字符串节点。
    • 传方法
      如果没有合适过滤器,那么还可以定义一个方法,方法只接受一个元素参数 [4] ,如果这个方法返回 True 表示当前元素匹配并且被找到,如果不是则反回 False。
  2. keyword参数
  3. text参数
  4. limit参数
  5. recursive参数

find( name , attrs , recursive , text , **kwargs )

它与 find_all() 方法唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果。

find_parents() find_parent()

find_all() 和 find() 只搜索当前节点的所有子节点,孙子节点等. find_parents() 和 find_parent() 用来搜索当前节点的父辈节点,搜索方法与普通tag的搜索方法相同,搜索文档搜索文档包含的内容。

find_next_siblings() find_next_sibling()

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

find_previous_siblings() find_previous_sibling()

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

find_all_next() find_next()

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

find_all_previous() 和 find_previous()

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

注:以上(2)(3)(4)(5)(6)(7)方法参数用法与 find_all() 完全相同,原理均类似,在此不再重复描述。

CSS选择器

我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list。

  1. 通过标签名查找

  2. 通过类名查找

  3. 通过id名查找

  4. 组合查找
    组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开

    print soup.select('p #link1')
    #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
    

    直接子标签查找

    print soup.select("head > title")
    #[<title>The Dormouse's story</title>]
    
  5. 属性查找
    查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。

    print soup.select('a[class="sister"]')
    #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister"     href="http://example.com/lacie" id="link2">Lacie</a>, <a        class="sister"     href="http://example.com/tillie" id="link3">Tillie</a>]
    
    print soup.select('a[href="http://example.com/elsie"]')
    #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
    

    同样,属性仍然可以与上述查找方式组合,不在同一节点的空格隔开,同一节点的不加空格。

    print soup.select('p a[href="http://example.com/elsie"]')
    #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
    

    以上的 select 方法返回的结果都是列表形式,可以遍历形式输出,然后用 get_text() 方法来获取它的内容。

    soup = BeautifulSoup(html, 'lxml')
    print type(soup.select('title'))
    print soup.select('title')[0].get_text()
    
    for title in soup.select('title'):
        print title.get_text()
    

    好,这就是另一种与 find_all 方法有异曲同工之妙的查找方法,是不是感觉很方便?

Reference

Beautiful Soup文档
Python爬虫利器二之Beautiful Soup的用法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值