我遇到了一些问题,我从this answer得到了这部分代码。这是我的代码:
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()
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)
如果我取消对测试打印的注释,我会得到类似于此的输出:
<?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>http://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>http://i.stack.imgur.com/dxPGis.jpg</small_square><large_thumbnail>http://i.stack.imgur.com/dxPGil.jpg</large_thumbnail></links></upload>
我希望接收一些特定的值,但它会给我一个AttributeError:
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但未产生预期结果的信息。在那种情况下,我得到一个TypeError:
Traceback (most recent call last): File "<pyshell#32>", line 34, in <module>
print 'Url: ' + url TypeError: cannot concatenate 'str' and 'NoneType' objects
我将str()添加到print中,现在我得到了一个很好的字符串。不好的是,现在我得到无响应。所以问题仍然适用。我如何获得此xml的网址,网页和delete_page?
答案 0 :(得分:2)
您的find()
来电正在尝试使用名为original
的标记找到树顶部的直接子项,而不是任何低于该标记的标记。使用:
url = tree.find('.//original').text
如果要查找树中名为original
的所有元素。 ElementTree的find()
方法的模式匹配规则在此页面的表格中列出:http://effbot.org/zone/element-xpath.htm
对于//
匹配,它说:
选择当前元素下所有级别的所有子元素(搜索整个子树)。例如,“.//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>http://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>http://i.stack.imgur.com/dxPGis.jpg</small_square>
<large_thumbnail>http://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
http://i.stack.imgur.com/dxPGi.png