V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Jan 23, 2013 · 3428 views
    This topic created in 4855 days ago, the information mentioned may be changed or developed.
    我是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 replies    1970-01-01 08:00:00 +08:00
    dreampuf
        1
    dreampuf  
       Jan 23, 2013   ❤️ 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
       Jan 23, 2013
    @dreampuf 十分感谢,我也刚刚发现,我之所以没取成功,是因为没指定命名空间。
    再一次感谢 @dreampuf :-)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2893 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 11:24 · PVG 19:24 · LAX 04:24 · JFK 07:24
    ♥ Do have faith in what you're doing.