V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
jezal
V2EX  ›  Python

请教elementTree解析XML的问题

  •  
  •   jezal · 2013-01-23 12:00:47 +08:00 · 2907 次点击
    这是一个创建于 4115 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我是python新手,最近研究xml解析。
    根据这个文档http://docs.python.org/2/library/xml.etree.elementtree.html里的例子中做了下,可以输出相应的结果。可是当我操作亚马逊的api返回的xml就无法解析,老是抓不到内容。

    请高手帮忙解析出下面这个xml里每个item里的图片和文字。
    xml地址:http://drips.me/api.xml 你可以下载下来使用。

    elementTree的官方文档貌似很粗糙,或者是我理解能力差,哪位朋友能帮忙给细说说elementTree查询元素的方法。谢谢了。

    这个问题困扰我两三天了,不然不会来这当伸手党,羞愧。
    2 条回复    1970-01-01 08:00:00 +08:00
    dreampuf
        1
    dreampuf  
       2013-01-23 13:55:25 +08:00   ❤️ 1
    el = etree.fromstring(urllib2.urlopen("http://drips.me/api.xml").read())
    for i in el.xpath('//xmlns:Item', namespaces={"xmlns": "http://webservices.amazon.com/AWSECommerceService/2011-08-01"}):
    print i
    jezal
        2
    jezal  
    OP
       2013-01-23 14:12:04 +08:00
    @dreampuf 十分感谢,我也刚刚发现,我之所以没取成功,是因为没指定命名空间。
    再一次感谢 @dreampuf :-)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3873 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 05:17 · PVG 13:17 · LAX 22:17 · JFK 01:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.