I am using the peak (API) API that returns an XML file. At the moment, I save it in an .xml file below:
req = urllib.orvection (url, headers = header) response data = urllib2.urlopen (req) .read () ofn = Asile: ofile.write (responseData) parse_xml ()
and then open it in parse_xml function as 'Pinnacle_feed_basketball.xml' Open (ONN, 'W')
< Pre> tree = etree.parse ("pinnacle_feed_basketball.xml") fdtime = tree.xpath ('// rsp / fd / fdTime / text ()')
I Saving it as an XML file and then reading it in the file is not necessary, but I have to work without doing this Can not get it for
I responseData
function
parse_xml (responseData)
and then in the function
tree = etree.parse (responseData) fdtime = tree.xpath ( '// rsp / fd / fdTime / text ()')
but it does not work. If you want to parse a memory obj Ect (in your case, a string),
etree.fromstring (& lt; obj & gt;) -
etree.parse
is a file-like object or file name expected - For example:
import urllib2, lxml.etree as etree url = 'http://www.xmlfiles.com/examples/note.xml' header = {} reiki = Urllib2.Request (url, headers = headers) response data = urllib2.urlopen (req) .read () element = etree.fromstring (responseData) print (element) print (etree.tostring (element, pretty_print = true))
Output:
& lt; Element note at 0x2c29dc8 & gt; & Lt; Comment & gt; & Lt; Gt; & gt; Toe & lt; / Gt; & Lt; To & gt; Known & lt; /> & Lt; Title & gt; Reminder & lt; / Heading & gt; & Lt; Body & gt; Do not forget me at the end of this week! & Lt; / Body & gt; & Lt; / Comment & gt;
No comments:
Post a Comment