python

PythonでXMLを操作する

みんなのPython でXMLを操作するサンプルがあったので実行してみました。
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

3.13.0 Documentation
ftp/python/2.5/python-2.5.msi ftp/python/2.5/Python-2.5.tar.bz2 pypi community/jobs psf/donations
PythonWebsiteCreatingNewTickets - Python Wiki
about/success/usa about/success/rackspace about/success/ilm about/success/astra about/success/honeywell about/success
Google
about/quotes
WebProgramming - Python Wiki
CgiScripts - Python Wiki
Welcome to Zope — Zope Project and Community documentation
Django
The web framework for perfectionists with deadlines.
The Web Framework that scales with you. — TurboGears2 Website 3.0 documentation
Python and XML Processing
The home page for XML processing with Python.
doc/topics/database
eGenix.com: mxODBC - Python ODBC Database Interface
mxODBC™ is the eGenix flagship product for connecting Python...
MySQL for Python
Download MySQL for Python for free. MySQL database connector...
doc/topics/database/modules
GuiProgramming - Python Wiki
WxPython - Python Wiki
TkInter - Python Wiki
PyGtk - Python Wiki
PyQt - Python Wiki
NumericAndScientific - Python Wiki
http://www.pasteur.fr/recherche/unites/sis/formation/python/index.html
http://www.pentangle.net/python/handbook/
community/sigs/current/edu-sig
Page Not Found
http://osl.iu.edu/~lums/swc/
about/apps
301 Moved Permanently
http://twistedmatrix.com/trac/
about/apps
The BuildBot
Edgewall Software: Trac
Edgewall is a highly experienced team of software developers...
Roundup Issue Tracker
Roundup is an issue-tracking system that boasts a user-frien...
IntegratedDevelopmentEnvironments - Python Wiki
about/apps
pygame news
301 Moved Permanently
500 Internal Server Error
about/apps psf/license psf
Join us at PyCon
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
ERROR: The request could not be satisfied
HTTP 429
psf about/legal >>>
タイトルとURLをコピーしました