Python 2.7では、xml.etree.ElementTreeモジュールを使用してXMLをパースすることができます。以下にその例を示します。

import urllib
import xml.etree.ElementTree as ET

url = raw_input('Enter location: ')
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
tree = ET.fromstring(data)
lst = tree.findall('.//count')
print 'Count:', len(lst)
total = 0
for comment in tree.findall("./comments/comment"):
    total += int(comment.find('count').text)
print total

このコードは、指定されたURLからXMLデータを取得し、そのデータをパースしています。findallメソッドを使用して、特定の要素(この場合はcount)を含むすべての要素を見つけ出します。その後、各comment要素のcountテキストを合計しています。

また、特定の値を取得するためには、以下のようにfindメソッドを使用します。

from xml.etree.ElementTree import *
import urllib2
import xml.etree.ElementTree as ET

isbn = raw_input("Enter IBSN Number Please ")
url = "https://bing.com/search?q=" % isbn
req = urllib2.Request(url)
response = urllib2.urlopen(req)
XmlData = response.read()
root = ET.fromstring(XmlData)
print(root.tag,root.attrib)
for child in root.find('item'):
    print child.tag
    print child.attrib
    print child.text

このコードは、指定されたISBN番号を用いてAPIからXMLデータを取得し、そのデータをパースしています。findメソッドを使用して、特定の要素(この場合はitem)を見つけ出します。

以上がPython 2.7でのXMLパーシングの基本的な方法です。これらのコードを参考に、自分のニーズに合わせてカスタマイズしてみてください。

投稿者 admin

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です