python3访问sina首页中文的处理方法

代码如下:

“””如果只用普通的import urllib.requesthtml = urllib.request.urlopen(“http://www.sina.com”).read()print(html.decode(‘gbk’))

出现下面的错误builtins.unicodedecodeerror: ‘gbk’ codec can’t decode byte 0x8b in position 1: illegal multibyte sequence

怎么办?原来是有的网站将网页用gzip压缩了 。请看下面的代码

建议大家用python2import urllib2from stringio import stringioimport gzip

request = urllib2.request(‘http://www.sina.com’)request.add_header(‘accept-encoding’, ‘gzip’)response = urllib2.urlopen(request)if response.info().get(‘content-encoding’) == ‘gzip’: buf = stringio( response.read()) f = gzip.gzipfile(fileobj=buf) data = f.read()print data.decode(“gbk”).encode(‘utf-8’)”””

import ioimport urllib.request as rimport gzipreq = r.request(“http://www.sina.com”, headers={“user-agent”: “mozilla/5.0 (macintosh; intel mac os x 10_9_1) applewebkit/537.36 (khtml, like gecko) chrome/31.0.1650.63 safari/537.36”, “accept-encoding”: “gzip”})bs = r.urlopen(req).read()bi = io.bytesio(bs)gf = gzip.gzipfile(fileobj=bi, mode=”rb”)print(gf.read().decode(“gbk”))

发表评论