python - Parsing compressed xml feed into ElementTree -
I am trying to parse the following feed in ElementTree in Python: "" (big file warning)
Whatever I have tried here:
feed = urllib.urlopen ("http://smarkets.s3.amazonaws.com/oddsfeed.xml") # Feed Compressed_data Compressed = Feed.read () Import StringIO compressedstream = StringIO.StringIO (compressed_data) Import gzip gzipper = gzip.GzipFile (fileobj = compressedstream) data = gzipper.read () #Parsx XML tree = ET.PERS (data)
But it just hangs on compressed_data = feed.read ()
Land is infinitely be ?? (I know that this is a big file, but I think it takes a lot longer than other non-compressed feeds, and it is massively killing any bandwidth benefit from gzip compression in the first place) .
Next I tried Request
,
url = "http://smarkets.s3.amazonaws.com/oddsfeed.xml" header = {With 'accept-encoding': 'gzip, deflate'} r = request.get (url, header = header, stream = true)
but now
< Pre> tree = ET Parse (r) .content)
or
tree = ET PRS (Rtext)
But these exception exceptions.
What is the proper way to do this?
The function "takes a file name or file object with XML data". You are giving it a full string of XML, it is going to try to open a file whose name is a big part of XML, probably there is no such file.
Do you want a function or constructor.
Or, if you want, you already have a file object, gzipper
; You can send it to parse
instead of just reading it in a string.
All this is covered by a brief in Docs:
We can import this data by reading it from a file:
import xml.etree.ElementTree as ET tree = ET. Pars ('country_data.xml') root = Tree.getroot ()
or directly with a string:
root = Et.fromstring (country_data_as_string)
Comments
Post a Comment