Python mechanize gzip response handling
Firstly, if you are not using SSL for all your sites, you should step it up! NameCheap's
SSL certificates
start at only $9. There's no excuse!
Firstly, if you have not tried Digital Ocean's
SSD Virtual Servers
for only $5/mo, I highly recommend them!
Mechanize is awesome. The documentation is shit. The gzip support is non-existent. Some sites like Yahoo! require gzip support.
def ungzipResponse(r,b):
headers = r.info()
if headers['Content-Encoding']=='gzip':
import gzip
gz = gzip.GzipFile(fileobj=r, mode='rb')
html = gz.read()
gz.close()
headers["Content-type"] = "text/html; charset=utf-8"
r.set_data( html )
b.set_response(r)
b = Browser()
b.addheaders.append( ['Accept-Encoding','gzip'] )
r = b.open('http://some-gzipped-site.com')
ungzipResponse(r,b)
print r.read()

No comments
Jump to comment form | comments rss [?] | trackback uri [?]