python 抓取页面

#coding=utf-8

import sys
import urllib2
import gzip
import StringIO

# 页面url
url = "http://china.toocle.com/company/show/pdetail--1000436--10532651.html"

# 页面编码
page_encode = "gbk"

request = urllib2.Request(url)
request.add_header("Accept-encoding", "gzip")
usock = urllib2.urlopen(request)
page = usock.read()

# 处理gzip过的页面
if usock.headers.get('content-encoding', None) == 'gzip':
    page = gzip.GzipFile(fileobj=StringIO.StringIO(page)).read()

# 转unicode(gbk/utf8)
if not isinstance(page, unicode):
    page = unicode(page, page_encode)

print(page)
2010-02-01 14:34 111 python spider