Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6006443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:32:23+00:00 2026-05-23T01:32:23+00:00

I have some code to retrieve XML data: import cStringIO import pycurl from xml.etree

  • 0

I have some code to retrieve XML data:

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()

The resulting data looks like:

<?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>

Now, following this answer, I’m trying to get some specific values from the data.

This is my attempt:

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)

I get an AttributeError if I try to add the .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'

I couldn’t find anything in Python’s help for ElementTree about this attribute. How can I get only the text, not the object?

I found some info about getting a text string here; but when I try it I get a TypeError:

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

If I try to print 'Url: ' + str(url) instead, there is no error, but the result shows as None.

How can I get the url, webpageanddelete_page` data from this XML?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:32:24+00:00Added an answer on May 23, 2026 at 1:32 am

    Your find() call is trying to find an immediate child of the top of the tree with a tag named original, not a tag at any lower level than that. Use:

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

    if you want to find all elements in the tree with the tag named original. The pattern matching rules for ElementTree’s find() method are laid out in a table on this page: http://effbot.org/zone/element-xpath.htm

    For // matching it says:

    Selects all subelements, on all levels beneath the current element (search the entire subtree). For example, “.//egg” selects all “egg” elements in the entire tree.

    Edit: here is some test code for you, it use the XML sample string you posted I just ran it through XML Tidy in TextMate to make it legible:

    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
    

    On my machine (OS X running python 2.6.1) that produces:

    Ian-Cs-MacBook-Pro:tmp ian$ python test.py 
    https://i.stack.imgur.com/dxPGi.png
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some existing code that retrieves data from a database using ADO.NET that
I am using the following code to retrieve data from the xml file. It
I have some code which collects points (consed integers) from a loop which looks
I have some code like this: If key.Equals(search, StringComparison.OrdinalIgnoreCase) Then DoSomething() End If I
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some code like this in a winforms app I was writing to
I have some code in a javascript file that needs to send queries back
I have some code that gives a user id to a utility that then
I have some code in an IAuthorizationFilter which redirects the user to a login
I have some code which utilizes parameterized queries to prevent against injection, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.