title: Decompressing an XML in python from an API request
body: <p>I am trying to use the API from <a href="https://neweden-dev.com/ZKillboard_API" rel="nofollow">https://neweden-dev.com/ZKillboard_API</a> in python.</p>
  
  <p>My code works up to this point:</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  import urllib2
  import gzip
  import zlib
  
  
  url = 'https://zkillboard.com/api/kills/characterID/95089021/xml/'
  request = urllib2.Request(url)
  request.add_header('Accept-encoding', 'gzip,deflate')
  
  data = urllib2.urlopen(request)
  </code></pre>
  
  <p>After this point, I get very confused.</p>
  
  <pre><code>contents = data.read()
  f = open("export.xml","w")
  f.write(contents)
  f.close()
  </code></pre>
  
  <p>Above was my first step. Opening the file made me realize that it was compressed (duh).</p>
  
  <p>So from this point I tried two things:</p>
  
  <pre><code>f = open("test.xml.gz", "w")
  f.write(data.read())
  f.close()
  g = gzip.open("test.xml.gz", "rb")
  file_content = g.read()
  g.close()
  </code></pre>
  
  <p>which gives me the error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\[filepath]", line 19, in &lt;module&gt;
      file_content = g.read()
    File "C:\Python27\lib\gzip.py", line 254, in read
      self._read(readsize)
    File "C:\Python27\lib\gzip.py", line 312, in _read
      uncompress = self.decompress.decompress(buf)
  error: Error -3 while decompressing: invalid distance too far back
  </code></pre>
  
  <p>My next step was to use zlib:</p>
  
  <pre><code>x = zlib.decompress(data.read())
  </code></pre>
  
  <p>which gives me the error of:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\[filepath]", line 15, in &lt;module&gt;
      x = zlib.decompress(data.read())
  error: Error -3 while decompressing data: incorrect header check
  </code></pre>
  
  <p>I know I am missing something super simple here, any thoughts? Thanks.</p>