开发者

Python parsing xml directly from web address

Hey. I tried to find a way but i can't. I have set up a xml.sax parser in python and it works perfect when i read a local file (for example calendar.xml), but i need to read a xml file from a web address.

I figured it would work if i do this:

toursxml='http://api.songkick.com/api/3.0/artists/mbid:'+mbid+'/calendar.xml?apikey=---------'
toursurl=urllib2.urlopen(toursxml)
toursurl=toursurl.read()
parser.parse(toursurl)

but it doesnt. im sure theres an easy way but i cant find it.

so yeah I can easily go to the url and download the file and open it by doing

parser.parse("calendar.xml")

as a work around ive set it 开发者_运维技巧up to read the file and create the file locally, close the file, and then read it. But as you can guess its slow as hell.

Is there anyone to directly read the xml? also note that the url name does not end in ".xml" so that may be a problem later


First, your example is mixed up. Please don't reuse variables.

toursurl= urllib2.urlopen(toursxml)
toursurl_string= toursurl.read()
parser.parseString( toursurl_string )

Reads the entire file into a string, named toursurl_string.

To parse a string, you use the parseString(toursurl_string) method.

http://docs.python.org/library/xml.sax.html#xml.sax.parseString

If you want to combine reading and parsing, you have to pass the "stream" or filename to parse.

toursurl= urllib2.urlopen(toursxml)
parser.parse(toursurl)


parser.parse(xyz)

expects xyz to be a file; you are looking for

parser.parseString(xyz)

which expects xyz to be a string containing XML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜