如何从 XML 数据中获取特定元素?

2023-12-24

我有一些代码来检索 XML 数据:

import cStringIO
import pycurl
from xml.etree import ElementTree

_API_KEY = 'my api key'
_ima = '/the/path/to/a/image'

sock = cStringIO.StringIO()

upl = pycurl.Curl()

values = [
            ("key", _API_KEY),
            ("image", (upl.FORM_FILE, _ima))]

upl.setopt(upl.URL, "http://api.imgur.com/2/upload.xml")
upl.setopt(upl.HTTPPOST, values)
upl.setopt(upl.WRITEFUNCTION, sock.write)
upl.perform()
upl.close()
xmldata = sock.getvalue()
#print xmldata
sock.close()

结果数据如下所示:

<?xml version="1.0" encoding="utf-8"?>
<upload><image><name></name><title></title><caption></caption><hash>dxPGi</hash><deletehash>kj2XOt4DC13juUW</deletehash><datetime>2011-06-10 02:59:26</datetime><type>image/png</type><animated>false</animated><width>1024</width><height>768</height><size>172863</size><views>0</views><bandwidth>0</bandwidth></image><links><original>https://i.stack.imgur.com/dxPGi.png</original><imgur_page>http://imgur.com/dxPGi</imgur_page><delete_page>http://imgur.com/delete/kj2XOt4DC13juUW</delete_page><small_square>https://i.stack.imgur.com/dxPGis.jpg</small_square><large_thumbnail>https://i.stack.imgur.com/dxPGil.jpg</large_thumbnail></links></upload>

现在,跟随这个答案 https://stackoverflow.com/questions/1140672/parsing-xml/1140753#1140753,我试图从数据中获取一些特定值。

这是我的尝试:

tree = ElementTree.fromstring(xmldata)
url = tree.findtext('original')
webpage = tree.findtext('imgur_page')
delpage = tree.findtext('delete_page')

print 'Url: ' + str(url)
print 'Pagina: ' + str(webpage)
print 'Link de borrado: ' + str(delpage)

我得到一个AttributeError如果我尝试添加.text access:

Traceback (most recent call last):
  File "<pyshell#28>", line 27, in <module>
    url = tree.find('original').text
AttributeError: 'NoneType' object has no attribute 'text'

我在Python的帮助中找不到任何内容ElementTree关于这个属性。如何只获取文本而不获取对象?

我找到了一些有关获取文本字符串的信息here http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.findtext;但是当我尝试它时,我得到一个类型错误:

Traceback (most recent call last): 
  File "<pyshell#32>", line 34, in <module>
    print 'Url: ' + url
TypeError: cannot concatenate 'str' and 'NoneType' objects

如果我尝试打印'Url: ' + str(url)相反,没有错误,但结果显示为None.

如何获取网址、网页and从该 XML 中删除_page`数据?


Your find()调用正在尝试查找树顶部的直接子级,其标签名为original,而不是比该级别更低的标签。使用:

url = tree.find('.//original').text

如果您想查找树中带有名为的标签的所有元素original。 ElementTree 的模式匹配规则find()方法在此页面的表格中列出:http://effbot.org/zone/element-xpath.htm http://effbot.org/zone/element-xpath.htm

For //匹配它说:

选择当前元素下所有级别上的所有子元素(搜索整个子树)。例如,“.//egg”选择整个树中的所有“egg”元素。

编辑:这里有一些测试代码,它使用您发布的 XML 示例字符串,我刚刚在 TextMate 中通过 XML Tidy 运行它以使其清晰:

from xml.etree import ElementTree
xmldata = '''<?xml version="1.0" encoding="utf-8"?>
<upload>
    <image>
        <name/>
        <title/>
        <caption/>
        <hash>dxPGi</hash>
        <deletehash>kj2XOt4DC13juUW</deletehash>
        <datetime>2011-06-10 02:59:26</datetime>
        <type>image/png</type>
        <animated>false</animated>
        <width>1024</width>
        <height>768</height>
        <size>172863</size>
        <views>0</views>
        <bandwidth>0</bandwidth>
</image>
<links>
    <original>https://i.stack.imgur.com/dxPGi.png</original>
    <imgur_page>http://imgur.com/dxPGi</imgur_page>
    <delete_page>http://imgur.com/delete/kj2XOt4DC13juUW</delete_page>
    <small_square>https://i.stack.imgur.com/dxPGis.jpg</small_square>
    <large_thumbnail>https://i.stack.imgur.com/dxPGil.jpg</large_thumbnail>
</links>
</upload>'''
tree = ElementTree.fromstring(xmldata)
print tree.find('.//original').text

在我的机器上(运行 python 2.6.1 的 OS X)会生成:

Ian-Cs-MacBook-Pro:tmp ian$ python test.py 
https://i.stack.imgur.com/dxPGi.png
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 XML 数据中获取特定元素? 的相关文章

随机推荐