Last active
June 30, 2017 16:21
-
-
Save lotusirous/fe4d5e211d3ab9c0d48c0362d66e3863 to your computer and use it in GitHub Desktop.
Example of urllib2 with gzip decompression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from StringIO import StringIO | |
| import requests | |
| import urllib2 | |
| import gzip | |
| headers = { | |
| 'Host':'pkget.com', | |
| 'Connection':'keep-alive', | |
| 'Accept':'application/json, text/javascript, */*; q=0.01', | |
| 'X-Requested-With':'XMLHttpRequest', | |
| 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', | |
| 'Referer':'https://pkget.com/igym.aspx', | |
| 'Accept-Encoding':'gzip, deflate, br', | |
| 'Accept-Language':'en-US,en;q=0.8,vi;q=0.6', | |
| 'Cookie':'ASP.NET_SessionId=aybobp3fnrwln5basjeov2hd; pkgetcom=lat0=25.054462174372386&lng0=121.5893840789795&lat1=25.04711415327437&lng1=121.51067733764648', | |
| } | |
| url = "https://pkget.com/getgym.ashx?a=25.054462174372386&b=121.5893840789795&c=25.04711415327437&d=121.51067733764648&e=0&f=&g=&h=&j=765" | |
| # print requests.get(url, headers= headers, timeout=5, verify=False).text | |
| # resource = urllib2.urlopen(urllib2.Request(url, headers=headers)).read() | |
| req = urllib2.Request(url, headers=headers) | |
| response = urllib2.urlopen(req) | |
| if response.info().get('Content-Encoding') == 'gzip': | |
| buf = StringIO(response.read()) | |
| f = gzip.GzipFile(fileobj=buf) | |
| pagedata = f.read() | |
| elif response.info().get('Content-Encoding') == 'deflate': | |
| pagedata = response.read() | |
| elif response.info().get('Content-Encoding'): | |
| print('Encoding type unknown') | |
| else: | |
| pagedata = response.read() | |
| # resource = urllib2.urlopen(urllib2.Request(url, headers=headers)) | |
| print pagedata |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment