ksaitoの日記

日々試したことの覚え書き

PythonでXMLを操作する

移転しました。

自動的にリダイレクトします。

みんなのPythonXMLを操作するサンプルがあったので実行してみました。
Ubuntuでは、ElementTreeもパッケージ化されているんですね。
下記で必要なパッケージが使えるようになります。

$ sudo aptitude install python-elementt

Pythonのパッケージの考え方を理解していなったので、本のサンプルの通りに実行できずにしばらく悩みました。
サンプルは、http://python.org/からリンクされているURLを一覧するプログラムのです。

$ python
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib import urlopen
>>> e = ElementTree(file=urlopen("http://python.org"))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'ElementTree' is not defined
>>>

ElementTreeをインポートしたら問題なく動きました。

$ python
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib import urlopen
>>> from elementtree.ElementTree import *
>>> e = ElementTree(file=urlopen("http://python.org"))
>>> for elem in e.getiterator():
...     if elem.tag.endswith("}a"):
...             print elem.get("href", "NONE")
...
.
#left-hand-navigation
#content-body
search
about
about/gettingstarted
about/apps
about/success
about/quotes
about/website
about/help
news
doc
download
community
psf
links
dev
download/releases/2.5
http://docs.python.org/
ftp/python/2.5/python-2.5.msi
ftp/python/2.5/Python-2.5.tar.bz2
pypi
community/jobs
psf/donations
http://wiki.python.org/moin/PythonWebsiteCreatingNewTickets
about/success/usa
about/success/rackspace
about/success/ilm
about/success/astra
about/success/honeywell
about/success
http://www.google.com/
about/quotes
http://wiki.python.org/moin/WebProgramming
http://wiki.python.org/moin/CgiScripts
http://www.zope.org/
http://www.djangoproject.com/
http://www.turbogears.org/
http://pyxml.sourceforge.net/topics/
doc/topics/database
http://www.egenix.com/files/python/mxODBC.html
http://sourceforge.net/projects/mysql-python
doc/topics/database/modules
http://wiki.python.org/moin/GuiProgramming
http://wiki.python.org/moin/WxPython
http://wiki.python.org/moin/TkInter
http://wiki.python.org/moin/PyGtk
http://wiki.python.org/moin/PyQt
http://wiki.python.org/moin/NumericAndScientific
http://www.pasteur.fr/recherche/unites/sis/formation/python/index.html
http://www.pentangle.net/python/handbook/
community/sigs/current/edu-sig
http://www.ibiblio.org/obp/pyBiblio/
http://osl.iu.edu/~lums/swc/
about/apps
http://www.amk.ca/python/howto/sockets/
http://twistedmatrix.com/trac/
about/apps
http://buildbot.sf.net
http://www.edgewall.com/trac/
http://roundup.sourceforge.net/
http://wiki.python.org/moin/IntegratedDevelopmentEnvironments
about/apps
http://www.pygame.org/news.html
http://www.alobbs.com/pykyra
http://www.vrplumber.com/py3d.py
about/apps
psf/license
psf
http://pycon.org/
about
download
download/releases/2.3.6
download/releases/2.3.6
download/releases/2.4.4
http://www.osdc.com.au
news/security/PSF-2006-001
download/releases/2.4.4
http://us.pycon.org/TX2007/CallForProposals
channews.rdf
about/website
http://www.xs4all.com/
http://www.pollenation.net/
psf
about/legal
>>>