使用python3解压缩URL

时间:2015-09-23 14:10:49

标签: python-3.x unzip

我正在尝试执行以下操作:

import zlib
import urllib.request

def decompress(url):
    f = urllib.request.urlopen(url)
    data = f.read()
    decompressed_data = zlib.decompress(data)
    return decompressed_data

URL = 'http://...../data.json.gz'

decompress(URL)

但是我受到了欢迎:

Traceback (most recent call last):
  File "zzz.py", line 14, in <module>
    decompress(URL)
  File "zzz.py", line 9, in decompress
    decompressed_data = zlib.decompress(data)
zlib.error: Error -3 while decompressing data: incorrect header check

如何用python3解压缩网址?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用gzip。我在下面的示例中使用requests,因为我更熟悉。另外,我正在使用示例json.gz数据文件。

# Python 3

import requests
import gzip

r = requests.get("https://wiki.mozilla.org/images/f/ff/Example.json.gz")
decompressed_data = gzip.decompress(r.content)

print(decompressed_data)

输出

b'{"InstallTime": "1295768962", "Comments": "Will test without extension.",     "Theme": "classic/1.0", "Version": "4.0b10pre", "id": "e
c8030f7-c20a-464f-9b0e-13a3a9e97384", "Vendor": "Mozilla",     "EMCheckCompatibility": "false", "Throttleable": "1", "Email": "deinspanj
er@mozilla.com", "URL": "http://nighthacks.com/roller/jag/entry/the_shit_finally_hits_the", "version":    "4.0b10pre", "CrashTime": "12
95903735", "ReleaseChannel": "nightly", "submitted_timestamp": "2011-01-24T13:15:48.550858", "buildid": "20110121153230", "timestamp
": 1295903748.551002, "Notes": "Renderers: 0x22600,0x22600,0x20400", "StartupTime": "1295768964", "FramePoisonSize": "4096", "FrameP
oisonBase": "7ffffffff0dea000", "AdapterRendererIDs":     "0x22600,0x22600,0x20400", "Add-ons": "compatibility@addons.mozilla.org:0.7,en
ter.selects@agadak.net:6,{d10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d}:1.3.3,sts-    ui@sidstamm.com:0.1,masspasswordreset@johnathan.nightingal
e:1.04,support@lastpass.com:1.72.0,{972ce4c6-7e08-4474-a285-    3208198ce6fd}:4.0b10pre", "BuildID": "20110121153230", "SecondsSinceLast
Crash": "810473", "ProductName": "Firefox", "legacy_processing": 0}'

希望这会有所帮助。