【爬虫学的好,基础少不了】:数据解析之BeautifulSoup4库

一、BeautifulSoup 4介绍

1.1 简介

简单来说,BeautifulSoup是python的一个库,最主要的功能是从网页抓取数据。官方解释如下:

Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据。

Beautiful Soup能将输入文档转换为Unicode编码,输出文档转换为UTF-8编码。如果文档没有说明编码方式, Beautiful Soup就需要自动设置编码方式。

1.2 解析库对比

序号 解析库 使用方法 优势 劣势
1 Python标准库(内置库) BeautifulSoup(html,“html.parser”) Python内置标准库,执行速度快 容错能力较差
2 lxml HTML解析库 BeautifulSoup(html,’lxml’) 速度快,容错能力强 需要安装,需要C语言库
lxml XML解析库 BeautifulSoup(html,[‘lxml’,’xml’]) 速度快,容错能力强,支持XML格式 需要C语言库
4 htm5lib解析库 BeautifulSoup(html,’htm5llib’) 以浏览器方式解析,最好的容错性 速度慢

二、BeautifulSoup 4基本使用

2.1 安装和文档

pip install Beautifulsoup4

中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

2.2 导入使用

form bs4 import BeautifulSoup

2.3 基础操作实例

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>
"""

soup = BeautifulSoup(html,'lxml')

# prettify() 方法 格式化输出 ; 将Beautiful Soup的文档树格式化后以Unicode编码输出,每个XML/HTML标签都独占一行
print(soup.prettify())

三、四大对象种类

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

序号 对象 作用
1 Tag BeautifulSoup中所有的标签都是Tag类型,并且BeautifulSoup的对象其实本质上也是一个Tag类型。所以其实一些方法比如find、find_all并不是BeautifulSoup的,而是Tag的。 tag对应Html中的标签
2 NavigableString 获取标签内的标签内部的文字,它继承自python中的str,用起来就跟使用python的str是一样的。
3 BeautifulSoup BeautifulSoup 对象表示的是一个文档的内容。也可以认为它是一个特殊的 Tag
4 Comment Comment 对象是一个特殊类型的 NavigableString 对象,可以获取文档中注释节点的内容。

3.1 Tag 实例

下面的 title head a p等等 HTML 标签加上里面包括的内容就是 Tag,,但是注意它查找的是在所有内容中的第一个符合要求的标签。

<head><title>The Dormouse's story</title></head>
<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>

获取tag

from bs4 import BeautifulSoup

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"></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>
<b><!--Hey, buddy. Want to buy a used parser?--></b>
"""

soup = BeautifulSoup(html,'lxml')

# 获取 tag
print(soup.p)  # <p class="title" name="dromouse"></p>

# 获取p标签的属性
print(type(soup.p)) # <class 'bs4.element.Tag'>

对于 Tag,它有两个重要的属性,是 name 和 attrs

 # 获取标签名
print(soup.p.name)  # p

# 获取标签名所有属性 
print(soup.p.attrs)  # {'class': ['title'], 'name': 'dromouse'}

 # 获取P标签下class的值
print(soup.p['class']) # ['title']
print(soup.p.get('class')) # ['title']

3.2 NavigableString 实例

获取标签内部的文字用 .string 即可,例如:

from bs4.element import NavigableString

# 获取a标签内的文字内容
print(soup.a.string)  # Elsie 

# 判断a.string的类型
print(type(soup.a.string)) # <class 'bs4.element.Comment'>

3.3 BeautifulSoup 实例

是一个特殊的 Tag,我们可以分别获取它的类型,名称,以及属性,例如:

# BeautifulSoup的类型,名称以及属性

print(type(soup))  
# <class 'bs4.BeautifulSoup'>

print(type(soup.name))  
# <class 'str'>

print(soup.name)  
# [document] ; 每个HTML文档都是Document。

print(soup.attrs)
# {} 文档本身的属性为空

3.4 Comment 实例

它继承NavigableString对象,所有使用方法和NavigableString对象一样,通过.string使用

print(soup.b)  # 此时不能出现空格和换行符,a标签如下:
# <b><!--Hey, buddy. Want to buy a used parser?--></b>

print(soup.b.string) # Hey, buddy. Want to buy a used parser?

print(type(soup.b.string)) # <class 'bs4.element.Comment'>



四、string和strings、stripped_strings属性以及get_text方法

  1. string:获取某个标签下的非标签字符串。返回来的是个字符串。如果这个标签下有多行字符,那么就不能获取到了。
  2. strings:获取某个标签下的子孙非标签字符串。返回来的是个生成器。
  3. stripped_strings:获取某个标签下的子孙非标签字符串,会去掉空白字符。返回来的是个生成器。
  4. get_text:获取某个标签下的子孙非标签字符串,以普通字符串形式返回

五、遍历文档树

5.1 直接子节点

5.1.1 content 属性
from bs4 import BeautifulSoup

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"></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>
<b>
<!--Hey, buddy. Want to buy a used parser?-->
</b>
"""

soup = BeautifulSoup(html,'lxml')

head_tag = soup.head

print(head_tag.contents)  # .contents 调用
# [<title>The Dormouse's story</title>]

print(type(head_tag.contents))
# <class 'list'>

5.1.2 children属性
print(type(head_tag.children))  # 列表迭代器 <class 'list_iterator'>

for i in head_tag.children:
    print(i)

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

5.1.3 contents和children的区别

都是返回某个标签下的直接子元素,其中也包括字符串。他们两的区别是:contents返回来的是一个列表,children返回的是一个迭代器。

5.2 所有子(孙)节点

5.2.1 descendants 属性

.descendants 属性可以对所有tag的子孙节点进行递归循环,和 children类似,我们也需要遍历获取其中的内容。

soup = BeautifulSoup(html,'lxml')
for child in soup.descendants:
    print(child)

运行结果

<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"></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>
<b>
<!--Hey, buddy. Want to buy a used parser?-->
</b>
</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"></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>
<b>
<!--Hey, buddy. Want to buy a used parser?-->
</b>
</body>


<p class="title" name="dromouse"></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>
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>
...


<b>
<!--Hey, buddy. Want to buy a used parser?-->
</b>


Hey, buddy. Want to buy a used parser?





5.3 返回所有字符类型

string:返回所有字符类型

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

# repr() 方法是一个函数,创建一个字符串,以合法python表达式的形式来表示值。,repr出来的值是给机器看的,能显示出文档内的换行符空格符等特殊字符

结果:

'\n'
None
'\n'
None
'Once upon a time there were three little sisters; and their names were\n'
' Elsie '
' Elsie '
',\n'
'Lacie'
'Lacie'
' and\n'
'Tillie'
'Tillie'
';\nand they lived at the bottom of a well.'
'\n'
'...'
'...'
'\n'
None
'\n'
'Hey, buddy. Want to buy a used parser?'
'\n'
'\n'

六、 搜索文档树

6.1 find与find_all的区别

**find:**找到第一个满足条件的标签就返回。说白了,就是只会返回一个元素。

find_all:将所有满足条件的标签都返回,会返回很多标签(以列表的形式)。

6.2 使用find和find_all的过滤条件

语法: find_all(name, attrs, recursive, text, **kwargs)

  1. 关键字参数:将属性的名字作为关键字参数的名字,以及属性的值作为关键字参数的值进行过滤。
  2. attrs参数:将属性条件放到一个字典中,传给attrs参数。
6.2.1 字符串过滤实例
from bs4 import BeautifulSoup
import re

html = """
<table class="tablelist" cellpadding="0" cellspacing="0">
    <tbody>
        <tr class="h" >
            <td class="l" width="374">职位名称</td>
            <td>职位类别</td>
            <td>人数</td>
            <td>地点</td>
            <td>发布时间</td>
            <td href='http://www.baidu.com>网址</td>
        </tr>
        <p>我是一个P标签</p>
        <p id="head">我是id为head的p标签</p>
  </tbody>
</table>
"""

soup = BeautifulSoup(html,'lxml')


# 1. 字符串过滤
trs = soup.find_all('td')
for tr in trs:
    print(tr) 
 
6.2.2 正则表达式过滤
# 正则表达式过滤: 如果传入的是正则表达式,那么BeautifulSoup4会通过search()来匹配内容
trs = soup.find_all(re.compile('td'))
for i in trs:
    print(i)
6.2.3 列表过滤
# 列表: 如果传入一个列表,BeautifulSoup4将会与列表中的任一元素匹配到的节点返回
t_list = soup.find_all(["td", "p"])
for item in t_list:
    print(item)

6.2.4 kwargs参数
# # 查询id=head的Tag
t_list = soup.find_all(id="head")


# # 查询所有包含class的Tag(注意:class在Python中属于关键字,所以加_以示区别)
t_list = soup.find_all(class_="h")
print(t_list)

七、select方法

使用以上方法可以方便的找出元素。但有时候使用css选择器的方式可以更加的方便。使用css选择器的语法,应该使用select方法。以下列出几种常用的css选择器方法:

7.1通过标签名查找:

print(soup.select('a'))

7.2 通过类名查找:

通过类名,则应该在类的前面加一个.。比如要查找class=sister的标签。示例代码如下:

print(soup.select('.sister'))

7.3 通过id查找:

通过id查找,应该在id的名字前面加一个#号。示例代码如下:

print(soup.select("#link1"))

7.4 组合查找:

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

print(soup.select("p #link1"))

直接子标签查找,则使用 > 分隔:

print(soup.select("head > title"))

7.5 通过属性查找:

查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。示例代码如下:

print(soup.select('a[href="http://example.com/elsie"]'))

7.7 获取内容

以上的 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())
发布了72 篇原创文章 · 获赞 79 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42444693/article/details/105260238