XML & HTML Python 構文解析ライブラリー lxmlをインストールする。
PytnonでXMLやHTMLを高速に処理するためのモジュール「lxml」をインストールする。
1.まず、lxmlに必要なパッケージをインストールして
yum install libxml2* libxslt*
2.lxmlパッケージをインストールする
easy_install lxml
3.それで、lxmlがインポート出来ればok!
python >>> import lxml >>>
4.すると、lxmlを使用して、こんな感じでHTMLやXMLの解析が出来たりします。
# python Python 2.4.3 (#1, Jun 18 2012, 08:55:31) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from urllib2 import * >>> import lxml.html >>> html = urlopen('http://www.yahoo.co.jp').read() >>> root = lxml.html.fromstring(html) >>> anchors = root.xpath('//a') >>> for anchor in anchors: ... print anchor.attrib['href'] (出力結果) r/mht s/136966 s/137653 s/137654 r/c1 : :