当urlopen()一个gbk页面时,在Python中编码问题

时间:2011-03-24 06:24:36

标签: python encoding

我的代码在这里:

# coding:utf-8

if __name__ == '__main__':
    from urllib2 import urlopen
    url = 'http://iccna.blog.sohu.com/164572951.html'
    data = urlopen(url).read()
    soup = BeautifulSoup(data,fromEncoding='gb18030')
    print WebExtractor(soup)

但在调试时,数据如下:

��5h�,��4�H�5��VM��\

我该怎么做才能获得BeautifulSoup的正确数据?谢谢!

1 个答案:

答案 0 :(得分:1)

问题是服务器返回Gzip压缩的数据。试试这个:

#-*- coding: utf-8 -*-
from __future__ import print_function

import gzip
import StringIO
import urllib2
from BeautifulSoup import BeautifulSoup

url = 'http://iccna.blog.sohu.com/164572951.html'
response = urllib2.urlopen(url)
data = response.read()
data = StringIO.StringIO(data)
gzipper = gzip.GzipFile(fileobj=data)
html = gzipper.read()
soup = BeautifulSoup(html, fromEncoding='gbk')
print(soup)

中文字符在我的系统上看起来仍然是错误的,但这可能会给你正确的方向。