pythonでxmlを処理する

ElementTreeを使う。


#! /usr/bin/env python                                                                                                                                                            
# -*- coding: utf-8 -*-                                                                                                                                                           

import urllib2
from xml.etree import ElementTree

xml = urllib2.urlopen("http://www.oricon.co.jp/api/ranking/xml/rankingdata.xml")
tree = ElementTree.parse(xml)
titles = tree.findall('.//ranking/data/item/packageInfo/title')
for title in titles:
    print title.text

実行結果

% python search.py
君は僕だ
ALL NIGHT LONG
Feel So Moon
超HAPPY SONG
愛でした。
愛でした。
Your Eyes
えれぴょん
Beautiful
オーバーライト/脳内Survivor
時のシルエット
リヴィング・シングス
ALIVE
オーヴァーエクスポーズド
VOCALIST VINTAGE
NEWS BEST
演歌名曲コレクション16〜櫻〜
Mr.Children 2005−2010<macro>
Mr.Children 2001−2005<micro>
L’Arc〜en〜Ciel Tribute
Kis−My−MiNT Tour at 東京ドーム 2012.4.8
コクリコ坂から
コクリコ坂から 横浜特別版
人志松本のすべらない話 聖夜スペシャル
新テニスの王子様 2
AKB48 リクエストアワーセットリストベスト100 2012
LIVE TWENITY
Bon Voyage
MOVIE23/ユニコーンツアー2011 ユニコーンがやって来る zzz...
ARASHI LIVE TOUR Beautiful World