通过后面网页下载器失去一个网页源代码的很长的字符串,接下来则是要通过网页解析器对网页源代码中的信息进行提取,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> and11<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;12and they lived at the bottom of a well.</p>1314<p class="story">...</p>15"""

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

1# 导入 beautifulsoup4 库、用于实现解析2from bs4 import BeautifulSoup34'''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.p10# <p class="title"><b>The Dormouse's story</b></p>1112# 获取第一个 p 元素的 class 属性13print beau_soup.p['class']14# [u'title']1516# 获取第一个 p 元素上面的 b 元素17print beau_soup.p.b18# <b>The Dormouse's story</b>1920# 获取 p 元素的父节点的源代码21print beau_soup.p.parent22'''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 were26<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> and28<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.name35# 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'''1213# 搜寻所有 p 元素、而后返回一个 p 元素的 list14print beau_soup.find_all('p')15# 搜寻所有 a 元素、而后返回一个 a 元素的 list16links = beau_soup.find_all('a')17for link in links:18    print '未爬取的链接:',link['href']1920# 多条件查找,获取 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'''2728# 通过 id 搜寻29print beau_soup.find(id='link3')3031# 多条件查找,获取 p 元素、并且 class 属性 == title 的元素32print beau_soup.find('p',class_='title')3334import re3536# 多条件查找,获取 a 元素的 href 属性中蕴含 lacie 字符串的元素对象37print beau_soup.find('a',href=re.compile(r"lacie"))

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

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

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