[python]HTML/XML解析器Beautiful Soup


【概要】
Beautiful Soupは、HTMLファイルまたはXMLファイルからデータを抽出できるPythonライブラリです.HTML/XMLの解析器です.
非仕様タグをうまく処理し、プロファイルツリーを生成することができます.簡単で一般的なナビゲーション(navigating)、プロファイルツリーの検索、および変更を提供します.プログラミング時間を大幅に節約できます.
【取付】
ダウンロード先:クリックしてリンクを開く
Linuxプラットフォームのインストール:
新しいDebainまたはubuntuを使用している場合は、システムのパッケージ管理でインストールできます.
$ apt-get install Python-bs4
Beautiful Soup 4はPyPiで公開されているので、システムパッケージを使用してインストールを管理できない場合はeasy_インストールまたはpipでインストールします.パッケージの名前はbeautifulsoup 4で、このパッケージはPython 2とPython 3に対応しています.
$ easy_install beautifulsoup4
$ pip install beautifulsoup4
(PyPiにはBeautifulSoupという名前のパッケージもありますが、それはあなたが望んでいるものではないかもしれません.それはBeautiful Soup 3のリリースバージョンです.多くのプロジェクトがBS 3を使用しているので、BeautifulSoupパッケージは依然として有効です.
しかし、新しいプロジェクトを作成している場合は、beautifulsoup 4をインストールする必要があります.
easyをインストールしていない場合はInstallまたはpipは、BS 4のソースコードをダウンロードしてsetupを通過することもできます.pyでインストールします.
$ Python setup.py install
上記のインストール方法が通じない場合、Beautiful Soupのリリースプロトコルでは、BS 4のコードをプロジェクトにパッケージ化することができ、インストールする必要がなく使用できます.
作者はPython 2.7とPython 3.2のバージョンでBeautiful Soupを開発し、理論的にはBeautiful Soupは現在のPythonのすべてのバージョンで正常に動作するはずです.
Windowsプラットフォーム:
ダウンロードが完了したら解凍する必要があります.D:/IT/Python 27の下に置くとします.
cmdを実行し、D:/IT/Python 27/beautifulsoup 4-4.3.2/ディレクトリに切り替えます(自分が解凍したディレクトリとダウンロードしたバージョン番号に基づいて変更します).
実行コマンド:python setup.py buildとpython setup.py install
インストールが完了します
【ケース】
以下のHTMLコードは例として複数回使用する.これはアリスの夢遊仙境の1段の内容です(後で内容の中でアリスのドキュメントと略称します):
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>

ドキュメントからすべての为了检索向拉伯尔的链接,遵循以下手顺。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. # # ... 【使用方法】链接