简体   繁体   中英

XML in Python and lxml

I am using the pinnacle (betting) api which returns an XML file. At the moment, I save it to a .xml file as below:

req = urllib2.Request(url, headers=headers)
responseData = urllib2.urlopen(req).read()

ofn = 'pinnacle_feed_basketball.xml'
with open(ofn, 'w') as ofile:
    ofile.write(responseData)
parse_xml()

and then open it in the parse_xml function

tree = etree.parse("pinnacle_feed_basketball.xml")
fdtime = tree.xpath('//rsp/fd/fdTime/text()')

I am presuming saving it as an XML file and then reading in the file is not necessary but I cannot get it to work without doing this.

I tried passing in responseData to the parsexml() function

parse_xml(responseData)

and then in the function

tree = etree.parse(responseData)
fdtime = tree.xpath('//rsp/fd/fdTime/text()')

But it doesn't work.

parse() is designed to read from file-like objects .

But you are passing a string in both cases - pinnacle_feed_basketball.xml string and responseData , which is also a string.

In the first case it should be:

with open("pinnacle_feed_basketball.xml") as f:
    tree = etree.parse(f)

In the second case:

root = etree.fromstring(responseData)  # note that you are not getting an "ElementTree" object here

FYI, urllib2.urlopen(req) is also a file-like object :

tree = etree.parse(urllib2.urlopen(req))

If you want to parse an in-memory object (in your case, a string), use etree.fromstring(<obj>) -- etree.parse expects a file-like object or filename -- Docs

For example:

import urllib2, lxml.etree as etree

url = 'http://www.xmlfiles.com/examples/note.xml'
headers = {}

req = urllib2.Request(url, headers=headers)
responseData = urllib2.urlopen(req).read()

element = etree.fromstring(responseData)
print(element)
print(etree.tostring(element, pretty_print=True))

Output:

<Element note at 0x2c29dc8>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM