みんなの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.5ftp/python/2.5/python-2.5.msi ftp/python/2.5/Python-2.5.tar.bz2 pypi community/jobs psf/donations3.13.0 Documentationabout/success/usa about/success/rackspace about/success/ilm about/success/astra about/success/honeywell about/successPythonWebsiteCreatingNewTickets - Python Wikiabout/quotesWebProgramming - Python WikiCgiScripts - Python WikiWelcome to Zope — Zope Project and Community documentationDjangoThe web framework for perfectionists with deadlines.The Web Framework that scales with you. — TurboGears2 Website 3.0 documentationdoc/topics/databasePython and XML ProcessingThe home page for XML processing with Python.eGenix.com: mxODBC - Python ODBC Database InterfacemxODBC™ is the eGenix flagship product for connecting Python...doc/topics/database/modulesMySQL for PythonDownload MySQL for Python for free. MySQL database connector...GuiProgramming - Python WikiWxPython - Python WikiTkInter - Python WikiPyGtk - Python WikiPyQt - Python WikiNumericAndScientific - Python Wikihttp://www.pasteur.fr/recherche/unites/sis/formation/python/index.htmlcommunity/sigs/current/edu-sighttp://www.pentangle.net/python/handbook/Page Not Foundabout/appshttp://osl.iu.edu/~lums/swc/301 Moved Permanentlyabout/appshttp://twistedmatrix.com/trac/The BuildBotEdgewall Software: TracEdgewall is a highly experienced team of software developers...Roundup Issue TrackerRoundup is an issue-tracking system that boasts a user-frien...about/appsIntegratedDevelopmentEnvironments - Python Wikipygame news301 Moved Permanentlyabout/apps psf/license psf500 Internal Server Errorabout download download/releases/2.3.6 download/releases/2.3.6 download/releases/2.4.4Join us at PyConnews/security/PSF-2006-001 download/releases/2.4.4http://www.osdc.com.auchannews.rdf about/websitehttp://us.pycon.org/TX2007/CallForProposalsERROR: The request could not be satisfiedpsf about/legal >>>HTTP 429