URLアクセスを行ってXPathで処理を行うサンプル
#-*- encoding: utf-8 -*- # URLアクセスをおこなってXPathで処理を行うサンプル import urllib2 import StringIO from xml.etree import ElementTree r = urllib2.urlopen('http://movapic.com/feed/user/fn7') # エラー処理はしない if(r.code != 200): exit(0) src = StringIO.StringIO(r.read()) xml = ElementTree.parse(src) items = xml.findall('//item') for item in items: link = item.findtext('./link') desc = item.findtext('./description') date = item.findtext('./pubDate') print([link,desc,date]) src.close()
DOMの時とちがって、出力される文字列がUTF8unicode文字列でないのはStringIOのせい?
[参考]
http://www.python.jp/doc/2.5/lib/elementtree-elementtree-objects.html
http://blog.cles.jp/item/2921
http://www.python.jp/doc/2.5/lib/module-StringIO.html
[追記]
StringIOにはunicode文字列をわたすこともできるらしい。だけど、どうやらetreeの処理でエラーが発生するみたい。
先程のコードでStringIOの生成を以下のようにしてみる。
# 正規表現ライブラリの読み込み import re # (中略) charset = re.findall('charest=(\S+)',r.info().typeheader)[0] src = StringIO.StringIO(unicode(r.read(),charset))
こうして実行してみると、エラー発生!!
Traceback (most recent call last): File "xpath_test.py", line 16, in <module> xml = ElementTree.parse(src) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py", line 862, in parse tree.parse(source, parser) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py", line 586, in parse parser.feed(data) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/etree/ElementTree.py", line 1245, in feed self._parser.Parse(data, 0) UnicodeEncodeError: 'ascii' codec can't encode characters in position 199-202: ordinal not in range(128)
うーんうーんと唸ったあと、itemタグの中のtitleタグも表示してみることにしてみたら、必要な文字に関してはunicode文字列になっていたようだった(笑)
以下、全ソース
#-*- encoding: utf-8 -*- # URLアクセスをおこなってXPathで処理を行うサンプル import urllib2 import StringIO import re from xml.etree import ElementTree r = urllib2.urlopen('http://movapic.com/feed/user/fn7') # エラー処理はしない if(r.code != 200): exit(0) src = StringIO.StringIO(r .read()) xml = ElementTree.parse(src) items = xml.findall('//item') for item in items: title = item.findtext('./title') link = item.findtext('./link') desc = item.findtext('./description') date = item.findtext('./pubDate') print([title,link,desc,date]) src.close()