我的代码在这里:
# 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 的正确数据?谢谢!
问题是服务器返回的是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)
汉字在我的系统上看起来仍然错误,但这可能会给你正确的方向。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)