关于python:认识爬虫beautifulsoup4-库如何使用三种方式提取-html-网页元素

8次阅读

共计 3154 个字符,预计需要花费 8 分钟才能阅读完成。

通过后面网页下载器失去一个网页源代码的很长的字符串,接下来则是要通过网页解析器对网页源代码中的信息进行提取,beautifulsoup4 库作为第三方插件同时反对 html、xml 的解析。通过将网页下载器下载的 html 字符串解析成为一个 BeautifulSoup 的对象,最初从这个对象中依据网页源代码的 html 标签、属性等因素提取咱们须要的内容。

1、筹备网页下载器获取的源代码

 1# 首先获取到网页下载器曾经下载到的网页源代码
 2# 这里间接取官网的案例
 3html_doc = """4<html><head><title>The Dormouse's story</title></head>
 5<body>
 6<p class="title"><b>The Dormouse's story</b></p>
 7
 8<p class="story">Once upon a time there were three little sisters; and their names were
 9<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
10<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
11<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
12and they lived at the bottom of a well.</p>
13
14<p class="story">...</p>
15"""

2、导入 beautifulsoup4 库并创立解析对象

1# 导入 beautifulsoup4 库、用于实现解析
2from bs4 import BeautifulSoup
3
4'''
5 创立 BeautifulSoup 对象、html_doc 为执行要解析的字符串、html.parser 为指定的解析器,
6 除此之外,还有其余的解析库, 比方 htm5llib、lxml, 各个解析库各有劣势
7'''8beau_soup = BeautifulSoup(html_doc,'html.parser')

3、应用结构化的形式获取元素、属性等

 1'''
 2 获取结构化元素或属性
 3'''
 4# 获取 title 元素、也就是 title 标签
 5print beau_soup.title
 6# <title>The Dormouse's story</title>
 7
 8# 获取第一个 p 元素
 9print beau_soup.p
10# <p class="title"><b>The Dormouse's story</b></p>
11
12# 获取第一个 p 元素的 class 属性
13print beau_soup.p['class']
14# [u'title']
15
16# 获取第一个 p 元素上面的 b 元素
17print beau_soup.p.b
18# <b>The Dormouse's story</b>
19
20# 获取 p 元素的父节点的源代码
21print beau_soup.p.parent
22'''
23<body>
24<p class="title"><b>The Dormouse's story</b></p>
25<p class="story">Once upon a time there were three little sisters; and their names were
26<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
27<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
28<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
29and they lived at the bottom of a well.</p>
30<p class="story">...</p>
31</body>
32'''
33# 获取 p 元素的父节点的名称
34print beau_soup.p.parent.name
35# body

4、通过元素搜寻的形式获取元素、属性等

 1'''
 2 除了通过结构化的形式获取元素,在其余状况应用结构化不容易获取元素时,3 能够应用相似于的搜寻的性能对源代码的标签、属性等进行筛选。4find() 函数、find_all() 函数能够利用多个条件的模式对源代码标签等
 5 进行搜寻。6'''7'''
 8find_all(self, name=None, attrs={}, recursive=True, text=None,
 9                 limit=None, **kwargs)
10 后果返回一个 list 汇合
11'''
12
13# 搜寻所有 p 元素、而后返回一个 p 元素的 list
14print beau_soup.find_all('p')
15# 搜寻所有 a 元素、而后返回一个 a 元素的 list
16links = beau_soup.find_all('a')
17for link in links:
18    print '未爬取的链接:',link['href']
19
20# 多条件查找, 获取 p 元素、并且 class 属性 == title 的元素
21print beau_soup.find_all('p',class_='title')
22'''
23 find(self, name=None, attrs={}, recursive=True, text=None,
24             **kwargs)
25 后果只返回一个,如果有多个则返回第一个,相比 find_all() 函数少了 limit 参数
26'''
27
28# 通过 id 搜寻
29print beau_soup.find(id='link3')
30
31# 多条件查找, 获取 p 元素、并且 class 属性 == title 的元素
32print beau_soup.find('p',class_='title')
33
34import re
35
36# 多条件查找, 获取 a 元素的 href 属性中蕴含 lacie 字符串的元素对象
37print beau_soup.find('a',href=re.compile(r"lacie"))

5、通过款式选择器的形式获取元素、属性等

1'''
2 除了上述应用结构化获取、元素 / 属性查找的形式,还提供了 select()
3 函数通过 css 款式选择器的形式进行元素获取, 这个函数返回的也是一个 list
4'''5print beau_soup.select('html head title')
6# html head title 在 css 选择器中示意 html 标签上面的 head 标签上面的 title 标签
7
8print beau_soup.select('#link3')
9# #link3 款式选择器中 id 为 link3 的元素

更多精彩返回微信公众号【Python 集中营】,关注获取《python 从入门到精通全套视频》

正文完
 0