html 树形结构_「手把手教python3接口自动化」:非结构化数据提取(三)

「第十三章」 非结构化数据提取(三)

13.6 BeautifulSoup4 解析器

和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据。

lxml 只会局部遍历,而Beautiful Soup是基于HTML DOM的,会载入整个文档,解析整个DOM树,因此时间和内存开销都会大很多,所以性能要低于lxml。

BeautifulSoup 用来解析 HTML 比较简单,API非常人性化,支持CSS选择器、Python标准库中的HTML解析器,也支持 lxml 的 XML解析器。

官方文档:http://beautifulsoup.readthedocs.io/zh_CN/v4.4.0

13.6.1 BeautifulSoup4的安装

用 pip 安装即可:pip install beautifulsoup4

1260bac7526508d4ba45a20f45096dc8.png

13.6.2 BeautifulSoup4的第一个示例

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html)

#格式化输出 soup 对象的内容

print(soup.prettify())

运行结果:

b438be137ce3f15738e7e5fd60c238cc.png

在 Python3 下执行,会看到这样一段警告:

BeautifulSoup(YOUR_MARKUP})

to this:

BeautifulSoup(YOUR_MARKUP, "lxml")

markup_type=markup_type))

意思是,如果我们没有显式地指定解析器,所以默认使用这个系统的最佳可用HTML解析器("lxml")。如果你在另一个系统中运行这段代码,或者在不同的虚拟环境中,使用不同的解析器造成行为不同。

我们可以通过soup = BeautifulSoup(html, "lxml")方式指定lxml解析器。

4d7d109c98995d466bf2b2b2dbae2b32.png

13.6.3 文件读取

在当前文件夹创建一个BeautifulSoup.html文件。

文件内容如下:

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

例子:

from bs4 import BeautifulSoup

#打开本地 HTML 文件的方式来创建对象

soup = BeautifulSoup(open('./BeautifulSoup.html'),"lxml")

#格式化输出 soup 对象的内容

print(soup.prettify())

运行结果:

09127ff1ef5136b5a863810f7c4634fd.png

13.6.4 各数据抓取工具的对比

学习了3种数据的抓取工具和方法,如下比较下这3种方法的优缺点。

25c0634306fd4d1e8cf89f045e486a71.png

13.6.5 BeautifulSoup四大对象种类

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

1.Tag

2.NavigableString

3.BeautifulSoup

4.Comment

(一)Tag

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

The Dormouse's story

The Dormouse's story

上面的 title head a p等等 HTML 标签加上里面包括的内容就是 Tag,那么试着使用 Beautiful Soup 来获取 Tags。

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(soup.title)

#

The Dormouse's story

print(soup.head)

#

The Dormouse's story

print(soup.a)

#

print(soup.p)

#

The Dormouse's story

print(type(soup.p))

#

运行结果:

e5be40ac5aaa91a2ba3f591b26d27ac4.png

我们可以利用 soup 加标签名轻松地获取这些标签的内容,这些对象的类型是bs4.element.Tag。但是注意,它查找的是在所有内容中的第一个符合要求的标签。如果要查询所有的标签,后面会进行介绍。

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

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(soup.name)

# [document] #soup 对象本身比较特殊,它的 name 即为 [document]

print(soup.head.name)

# head #对于其他内部标签,输出的值便为标签本身的名称

print(soup.p.attrs)

# {'class': ['title'], 'name': 'dromouse'}

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

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

# ['title'] #还可以利用get方法,传入属性的名称,二者是等价的

soup.p['class'] = "newClass"

print(soup.p) # 可以对这些属性和内容等等进行修改

#

The Dormouse's story

del soup.p['class'] # 还可以对这个属性进行删除

print(soup.p)

#

The Dormouse's story

运行结果:

[document]

head

{'class': ['title'], 'name': 'dromouse'}

['title']

The Dormouse's story

The Dormouse's story

(二)NavigableString

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

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(soup.p.string)

# The Dormouse's story

print(type(soup.p.string))

# In [13]:

运行结果:

c7cd5f5dda893c01a4110d2954e93e6f.png

(三)BeautifulSoup

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

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(type(soup.name))

#

print(soup.name)

# [document]

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

# {}

运行结果:

f9e834034e03717e2e5a256d63beac66.png

(四)Comment

Comment 对象是一个特殊类型的 NavigableString 对象,其输出的内容不包括注释符号。

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(soup.a)

#

print(soup.a.string)

# Elsie

print(type(soup.a.string))

#

运行结果:

f36883406eec675f2bd7f12f72984b7e.png

a 标签里的内容实际上是注释,但是如果我们利用 .string 来输出它的内容时,注释符号已经去掉了。

13.6.6 遍历文档树

1. 直接子节点 :.contents 和 .children 属性

.content 属性

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

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(soup.head.contents)

#[

The Dormouse's story]

运行结果:

eb32de081ce7738b964dea97927369df.png

[

The Dormouse's story]

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

Print(soup.head.contents[0])

#

The Dormouse's story

.children 属性

它返回的不是一个 list,不过我们可以通过遍历获取所有子节点。

我们打印输出 .children 看一下,可以发现它是一个 list 生成器对象。

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(soup.head.children)

#

for child in soup.body.children:

print(child)

运行结果:

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

2. 所有子孙节点: .descendants 属性

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

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

for child in soup.descendants:

print(child)

运行结果:

The Dormouse's story

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

Once upon a time there were three little sisters; and their names were

Elsie

Lacie

Lacie

and

Tillie

Tillie

and they lived at the bottom of a well.

...

...

3. 节点内容: .string 属性

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

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

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(soup.head.string)

#The Dormouse's story

print(soup.title.string)

#The Dormouse's story

运行结果:

The Dormouse's story

The Dormouse's story

13.6.7 搜索文档树

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

1)name 参数

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

A.传字符串

最简单的过滤器是字符串.在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容,下面的例子用于查找文档中所有的标签:

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

soup.find_all('b')

print(soup.find_all('a'))

运行结果:

8beeee67665373445ad2f02f2b778156.png

B.传正则表达式

如果传入正则表达式作为参数,Beautiful Soup会通过正则表达式的 match() 来匹配内容.下面例子中找出所有以b开头的标签,这表示

标签都应该被找到。

例子:

from bs4 import BeautifulSoup

import re

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

for tag in soup.find_all(re.compile("^b")):

print(tag.name)

运行结果:

body

b

C.传列表

如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有标签和标签。

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(soup.find_all(["a", "b"]))

for i in soup.find_all(["a", "b"]):

print(i)

运行结果:

6fde15654ca58414dc8d5fa7a367b878.png

2)keyword 参数

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(soup.find_all(id='link2'))

运行结果:

[Lacie]

3)text 参数

通过 text 参数可以搜搜文档中的字符串内容,与 name 参数的可选值一样, text 参数接受(字符串/正则表达式/列表)。

例子:

from bs4 import BeautifulSoup

import re

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(soup.find_all(text="Elsie"))

# ['Elsie']

print(soup.find_all(text=["Tillie", "Elsie", "Lacie"]))

# ['Elsie', u'Lacie', u'Tillie']

print(soup.find_all(text=re.compile("Dormouse")))

["The Dormouse's story", u"The Dormouse's story"]

运行结果:

[]

['Lacie', 'Tillie']

["The Dormouse's story", "The Dormouse's story"]

13.7 css选择器

要使用css对HTML页面中的元素实现一对一,一对多或者多对一的控制,这就需要用到CSS选择器。

HTML页面中的元素就是通过CSS选择器进行控制的。

CSS选择器也是基于BeautifulSoup里的select()方法,与 find_all 方法使用类似,不过也是有以下2点区别。

1.写 CSS 时,标签名不加任何修饰,类名前加.,id名前加#

2.在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list

13.7.1 CSS选择器分类

eb74e14a0a7bc8761dd2843b98dea47a.png

13.7.2 选择器语法

1、基本选择器语法。

541d2452ef20ba834be1500cf16aa159.png

2、层次选择器语法。

7b2270ab9f8db873e364e0cd3cd3be47.png

3、动态伪类选择器语法。

efe8491df450ca5b0089483c51d9f843.png

4、目标伪类选择器。

167af5e69df5376b157a8e7155a693ef.png

5、UI元素状态伪类选择器语法。

060f1719a8e4f4b2fd88668599efdccf.png

6、否定伪类选择器。

c2e0ab58ee38e5e788d47813b33dc571.png

7、属性选择器语法。

e798abe553d036802ac6c94e8e3d8cfd.png

注:例

13.7.3 通过标签名查找

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

print(soup.select('title'))

print(soup.select('a'))

print(soup.select('b'))

运行结果:

d08d108916656bd4db49009c45c76d6b.png

13.7.4 通过类名查找

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

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

运行结果:

34a003da3f9039a11a991d7e7f7c8a28.png

13.7.5 通过 id 名查找

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

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

运行结果:

3f5296fdbd40d3542f943dc1a08dcdc5.png

13.7.6 组合查找

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

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

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

运行结果:

f71652a58db0735c088663c8b3fc56f8.png

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

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

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

运行结果:

[

The Dormouse's story]

13.7.7 属性查找

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

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

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

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

运行结果:

17887df0b4a97f81a7082ac188b3e93d.png

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

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

soup = BeautifulSoup(html,"lxml")

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

运行结果:

fc6f588524f21416092e3627ae4dbc9b.png

13.7.8 获取内容

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

例子:

from bs4 import BeautifulSoup

html = """

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

#创建 Beautiful Soup 对象

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())

运行结果:

The Dormouse's story

The Dormouse's story

13.8 使用BeautifulSoup4 进行爬虫综合案例

腾讯社招页面:http://hr.tencent.com/position.php?&start=10#a

604160dabc461f5c2bc4fbadadabdcbc.png

使用BeautifuSoup4解析器,将招聘网页上的职位名称、职位类别、招聘人数、工作地点、发布时间,以及每个职位详情的点击链接存储出来。

6009e62ae95ea8bea8529d29d3a0b819.png

代码的实现:

from bs4 import BeautifulSoup

import json # 使用了json格式存储

from urllib import parse

import requests

from lxml import etree

def tencent():

url = 'http://hr.tencent.com/'

header = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1 Trident/5.0;"}

req = requests.get(url + 'position.php?&start=10#a',headers=header)

resHtml = req.text

output =open('./tencent.json','w+')

html = BeautifulSoup(resHtml,'lxml')

# 创建CSS选择器

result = html.select('tr[class="even"]')

result2 = html.select('tr[class="odd"]')

result += result2

items = []

for site in result:

item = {}

name = site.select('td a')[0].get_text()

detailLink = site.select('td a')[0].attrs['href']

catalog = site.select('td')[1].get_text()

recruitNumber = site.select('td')[2].get_text()

workLocation = site.select('td')[3].get_text()

publishTime = site.select('td')[4].get_text()

item['name'] = name

item['detailLink'] = url + detailLink

item['catalog'] = catalog

item['recruitNumber'] = recruitNumber

item['publishTime'] = publishTime

items.append(item)

# 禁用ascii编码,按utf-8编码

line = json.dumps(items,ensure_ascii=False)

output.write(line)

output.close()

if __name__ == "__main__":

tencent()

运行结果:

1cefe67fcce201cbf72179183aa3f7b4.png

在当前文件夹目录多个tencent.json的文件。

4b70c17c1e401b19388b2f65a5a7046c.png

用EditPlus 3查看如下:

1d6e51100f90bceaede644978ee1094b.png

把返回的数据通过json线上解析工具对返回的JSON数据解析,我们可以直观的看出。

4c3340efa9d5541734704504d3311e21.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值