2009年11月17日火曜日

urllib2でgzipされたデータを扱う

ここに書いてあった。
http://diveintomark.org/projects/misc/httpgzip.py.txt

StringIOよりcStringIOのが高速でよいと聞いたのでそちらを使ってみる。
それ以外はまるパクリ。



import urllib2, gzip, cStringIO

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=cStringIO.StringIO(data)).read()
return data

if __name__ == '__main__':
import sys
uri = sys.argv[1:] and sys.argv[1] or 'http://mixi.jp/'
print get(uri)

1 件のコメント:

  1. But they shaped 점보카지노 a partnership with Par-A-Dice Casino which gained them entry into the profitable and increasingly crowded Illinois betting market. A 2020 research of Kenyan student bettors by Ogachi identified almost seven out of 10 with playing problems. Nelson Bwire, who led the Kenyatta University poll while an undergraduate, was so alarmed he based a nonprofit, the Gaming Awareness Society of Kenya, that seeks minimize back} playing hurt. Bwire has recommended college students who’ve been compelled to go away college after betting away their tuition, and staff who’ve been jailed for squandering their employers’ cash. Nearly $60 million has been spent — much of it by giant Indigenous tribes — to assist campaigns for Proposition 26, which, if enacted, will enable solely tribal casinos to supply in-person betting. California casinos make up much of the $50 million marketing campaign towards Prop 26.

    返信削除