文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>python gzip get url

python gzip get url

时间:2010-08-23  来源:lexus

import urllib2, gzip, StringIO

__author__ = "Mark Pilgrim ([email protected])"
__license__ = "Python"

def get(uri):
    request = urllib2.Request(uri)
    request.add_header("Accept-encoding", "gzip")
    usock = urllib2.urlopen(request)
    data = usock.read()
    if usock.headers.get('content-encoding', None) == 'gzip':
        data = gzip.GzipFile(fileobj=StringIO.StringIO(data)).read()
    return data

if __name__ == '__main__':
    import sys
    uri = sys.argv[1:] and sys.argv[1] or 'http://leknor.com/'
    print get(uri)
<div>
               <div><h3 class="title">Example 11.12. Using the redirect handler to detect permanent redirects</h3>
 http://diveintopython.org/http_web_services/redirects.html                 <h2 class="title"><a name="oa.gzip"></a>11.8. Handling compressed data</h2></div></div>
http://diveintopython.org/http_web_services/gzip_compression.html<br />
<br />
http://rationalpie.wordpress.com/2010/06/02/python-streaming-gzip-decompression/<br />
<br />
<br />
<br />
<div class="primary">
                                <h1>Python mechanize gzip response handling</h1>
                                <p>Mechanize is awesome. The documentation is shit. The gzip support is non-existent. Some sites like Yahoo! require gzip support.</p>
<pre>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()

http://unformatt.com/news/python-mechanize-gzip-response-handling/

http://news.ycombinator.com/item?id=1424488 good article http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/
相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载