爬虫---Beautiful Soup库

来源:互联网 发布:php indexof 字符串 编辑:程序博客网 时间:2024/06/10 07:13

Beautiful Soup 库一般被称为bs4库,支持Python3,是我们写爬虫非常好的第三方库。因用起来十分的简便流畅。所以也被人叫做“美味汤”。官方文档

安装方式:

$ pip install beautifulsoup4
继续上一节中的pip list查看是否安装成功。

简单使用:

下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档):

html_doc = """<html><head><title>The Dormouse's story</title></head><body><p class="title"><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解析这段代码,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出:

>>> from bs4 import BeautifulSoup>>> html_doc = """... <html><head><title>The Dormouse's story</title></head>... <body>... <p class="title"><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_doc,'html.parser')>>> print(soup.prettify())<html> <head>  <title>   The Dormouse's story  </title> </head> <body>  <p class="title">   <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

bs4库就是将网页文件变成了一个soup的类型,事实上,bs4库 是解析、遍历、维护、“标签树“的功能库。 通俗一点说就是: bs4库把html源代码重新进行了格式化, 从而方便我们对其中的节点、属性、标签、进行操作。

接下来采用soup的相关操作来浏览结构化的数据方式

soup.title# <title>The Dormouse's story</title># 文档的标题
soup.title.name# u'title'# 标题的name
soup.title.string# u'The Dormouse's story'# 标题中的字符串
soup.title.parent.name# u'head'# title父亲节点的name属性
soup.p# <p class="title"><b>The Dormouse's story</b></p># 文档第一个找到的段落
soup.p['class']# u'title'# 第一个找到的段落的class属性值
soup.a# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a># 找到a的标签
soup.find_all('a')# [<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>]# 找到所有a的标签
soup.find(id="link3") # 找到id值等于3的a的标签
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a> 

上面的例子,我们知道bs4库这样理解一个html源文件:

1.首先,将html源文件转换为soup类型

2.从soup类型中通过特定方式抓取内容


一些比较经常用到的用法:

从文档中找到所有<a>标签的链接:

#发现了没有,find_all方法返回的是一个可以迭代的列表for link in soup.find_all('a'):    print(link.get('href'))    # http://example.com/elsie    # http://example.com/lacie    # http://example.com/tillie
从文档中获取所有文字内容:

#我们可以通过get_text 方法 快速得到源文件中的所有text内容。print(soup.get_text())# The Dormouse's story## The Dormouse's story## Once upon a time there were three little sisters; and their names were# Elsie,# Lacie and# Tillie;# and they lived at the bottom of a well.## ...

声明:原作者链接如下!

每天的学习记录都会 同步更新到: 微信公众号: findyourownway 知乎专栏:https://zhuanlan.zhihu.com/Ehco-python blog : www.ehcoblog.ml

原创粉丝点击