Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1073719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:01:38+00:00 2026-05-16T21:01:38+00:00

I use the following python code to download web pages from servers with gzip

  • 0

I use the following python code to download web pages from servers with gzip compression:

url = "http://www.v-gn.de/wbb/"
import urllib2
request = urllib2.Request(url)
request.add_header('Accept-encoding', 'gzip')
response = urllib2.urlopen(request)
content = response.read()
response.close()

import gzip
from StringIO import StringIO
html = gzip.GzipFile(fileobj=StringIO(content)).read()

This works generally, but for the specified URL fails with a struct.error exception.
I get a similar result if I use wget with an “Accept-encoding” header. However, browsers seem to be able to decompress the response.

So my question is: is there a way I can get my python code to decompress the HTTP response without resorting to disabling compression by removing the “Accept-encoding” header?

For completeness, here’s the line I use for wget:

wget --user-agent="Mozilla" --header="Accept-Encoding: gzip,deflate" http://www.v-gn.de/wbb/
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:01:38+00:00Added an answer on May 16, 2026 at 9:01 pm

    It appears you can call readline() on the gzip.GzipFile object, but
    read() raises a struct.error because the file ends abruptly.

    Since readline works (except at the very end), you could do something like this:

    import urllib2
    import StringIO
    import gzip
    import struct
    
    url = "http://www.v-gn.de/wbb/"
    request = urllib2.Request(url)
    request.add_header('Accept-encoding', 'gzip')
    response = urllib2.urlopen(request)
    content = response.read()
    response.close()
    fh=StringIO.StringIO(content)
    html = gzip.GzipFile(fileobj=StringIO.StringIO(content))
    try:
        for line in html:
            line=line.rstrip()
            print(line)
    except struct.error:
        pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually use the following Python code to read lines from a file :
Consider the following Python code: import os print os.getcwd() I use os.getcwd() to get
I have the following python code: import pty import subprocess os=subprocess.os from subprocess import
I have the following Python (3.2) code: from pygame import * class Application: def
As of now I use the following python code: file = open(filePath, r) lines=file.readlines()
I'm trying to use Python to download the HTML source code of a website
I have the following python code: row = conn.execute('''SELECT admin FROM account WHERE password
I use the following method to break the double loop in Python. for word1
In python, I can do the following, to conditionally use a 2nd list if
I use following code: Create a retry policy, when error, retry after 1 second,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.