python BeautifulSoup 库 笔记

来源:互联网 发布:99客服软件下载 编辑:程序博客网 时间:2024/06/05 19:02

本文章来自:
静觅 » Python爬虫入门八之Beautiful Soup的用法
并且经过我一定的修改

1. Beautiful Soup的简介

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

Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序。
Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码。你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,Beautiful Soup就不能自动识别编码方式了。然后,你仅仅需要说明一下原始编码方式就可以了。
Beautiful Soup已成为和lxml、html6lib一样出色的python解释器,为用户灵活地提供不同的解析策略或强劲的速度。

2. Beautiful Soup官方文档

中文4.2版
英文4.4版

官方文档中的快速开始示例:

下面的一段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 BeautifulSoupsoup = BeautifulSoup(html_doc)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="link2">#     Tillie#    </a>#    ; and they lived at the bottom of a well.#   </p>#   <p class="story">#    ...#   </p>#  </body># </html>

几个简单的浏览结构化数据的方法:

soup.title# <title>The Dormouse's story</title>soup.title.name# u'title'soup.title.string# u'The Dormouse's story'soup.title.parent.name# u'head'soup.p# <p class="title"><b>The Dormouse's story</b></p>soup.p['class']# u'title'soup.a# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</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>]soup.find(id="link3")# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

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

for link in soup.find_all('a'):    print(link.get('href'))    # http://example.com/elsie    # http://example.com/lacie    # http://example.com/tillie

从文档中获取所有文字内容:

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.## ...

4. find_all()

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

find_all() 方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件.这里有几个例子:

soup.find_all("title")# [<title>The Dormouse's story</title>]soup.find_all("p", "title")# [<p class="title"><b>The Dormouse's story</b></p>]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>]soup.find_all(id="link2")# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]import resoup.find(text=re.compile("sisters"))# u'Once upon a time there were three little sisters; and their names were\n'

有几个方法很相似,还有几个方法是新的,参数中的 textid 是什么含义? 为什么 find_all("p", "title") 返回的是CSS Class为”title”的

标签? 我们来仔细看一下 find_all() 的参数

0 0
原创粉丝点击