kaola4549 发表于 2017-5-1 14:49:12

[Python]HTML/XML解析器Beautiful Soup

【简介】
  Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库。即HTML/XMLX的解析器。
  

它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作。它可以大大节省你的编程时间。
【安装】
  下载地址:点击打开链接
  Linux平台安装:
  如果你用的是新版的Debain或ubuntu,那么可以通过系统的软件包管理来安装:
  $apt-getinstallPython-bs4
  Beautiful Soup 4 通过PyPi发布,所以如果你无法使用系统包管理安装,那么也可以通过easy_install或pip来安装.包的名字是beautifulsoup4,这个包兼容Python2和Python3.
  $easy_installbeautifulsoup4
  $pipinstallbeautifulsoup4
  (在PyPi中还有一个名字是BeautifulSoup的包,但那可能不是你想要的,那是Beautiful
Soup3的发布版本,因为很多项目还在使用BS3, 所以BeautifulSoup包依然有效。
  但是如果你在编写新项目,那么你应该安装的beautifulsoup4)
  如果你没有安装easy_install或pip,那你也可以下载BS4的源码,然后通过setup.py来安装。
  $Pythonsetup.pyinstall
  如果上述安装方法都行不通,Beautiful Soup的发布协议允许你将BS4的代码打包在你的项目中,这样无须安装即可使用.
  作者在Python2.7和Python3.2的版本下开发Beautiful Soup, 理论上Beautiful Soup应该在所有当前的Python版本中正常工作
  windows平台:

下载完成之后需要解压缩,假设放到D:/IT/Python27下。

运行cmd,切换到D:/IT/Python27/beautifulsoup4-4.3.2/目录下(根据自己解压缩后的目录和下载的版本号修改)。

运行命令:python setup.py build和python setup.py install
  即可安装完毕<前提是你的python路径加入到环境变量中了>





【案例】
  下面的一段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
soup = 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>

从文档中找到所有<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.
#
# ...

【如何使用】

链接地址:点击打开链接











  

  

  

  

  

  
页: [1]
查看完整版本: [Python]HTML/XML解析器Beautiful Soup