I was trying out the bit.ly api for shorterning and got it to work. It returns to my script an xml document. I wanted to extract out the tag but cant seem to parse it properly.
askfor = urllib2.Request(full_url)
response = urllib2.urlopen(askfor)
the_page = response.read()
So the_page contains the xml document. I tried:
from xml.dom.minidom import parse
doc = parse(the_page)
this causes an error. what am I doing wrong?
You don’t provide an error message so I can’t be sure this is the only error. But,
xml.minidom.parsedoes not take a string. From the docstring forparse:You should try:
since
responsewill behave like a file object. Or you could use theparseStringmethod inminidominstead (and then passthe_pageas the argument).EDIT: to extract the URL, you’ll need to do:
The result of
getElementsByTagNameis a list of all nodes matching (just one in this case).urlis an Element as you noticed, which contains a child Text node, which contains the data you need.