0

I'm trying to parse this: http://www.codespot.blogspot.in/atom.xml?redirect=false&start-index=1&max-results=500

Problem being:

  1. I've to store the xml in a file for ElementTree to parse it. How to avoid it and just parse the string response from the GET request?

  2. Though I'm doing this, to get all the titles, it still doesn't work:

    f = open('output.xml','wb+')
        f.write(r.content)
        f.close()
        tree = ""
        with open('output.xml', 'rt') as f:
            tree = ElementTree.parse(f)
            print tree
            root = tree.getroot()
            for elem in tree.iter():
                print elem.tag, elem.attrib
            for atype in tree.findall('title'):
                print atype.contents
    
3
  • Explain "doesn't work". Commented Apr 17, 2013 at 4:57
  • Shows up empty. I want the titles and content of each post. Commented Apr 17, 2013 at 5:03
  • To parse a string and not a file, you use ElementTree.fromstring(string), but you don't need to do that. namit found the correct namespace use before me. :) Commented Apr 17, 2013 at 5:11

1 Answer 1

2
import urllib2
from xml.etree import cElementTree as ET
conn = urllib2.urlopen("http://www.codespot.blogspot.in/atom.xml?redirect=false&start-index=1&max-results=500")
myins=ET.parse(conn)
for elem in myins.findall('{http://www.w3.org/2005/Atom}entry/{http://www.w3.org/2005/Atom}title'):
    print elem.text

or to find the both title and content::

for elem in myins.findall('{http://www.w3.org/2005/Atom}entry'):
    print elem.find('{http://www.w3.org/2005/Atom}title').text ## this will be the title
    print elem.find('{http://www.w3.org/2005/Atom}content').text ## this will be the content
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.