python抓网页的编码问题

来源:百度知道 编辑:UC知道 时间:2024/05/28 06:19:53
小弟刚接触python,抓第一个网页就过不去编码这个坎`请高手赐教,代码如下:

#coding = gb2312
import urllib2
from BeautifulSoup import BeautifulSoup

outfile = open("lsxk.txt", "w")
for i in range(1,2): #第一页
url= "http://bbs.scu.edu.cn/wForum/disparticle.php?
boardName=SCUExpress&ID=1735295349&pos=-1&page=%d" % i
doc = urllib2.urlopen(url).read()
soup = BeautifulSoup(doc,fromEncoding="gb2312")
print >> outfile,doc #输出到文本
outfile.close()

抓到txt中的内容是中文,但使用beautifulSoup后就乱码了`
BS的文档说使用soup = BeautifulSoup(doc, fromEncoding="gb2312"),
还是不能正确编码.print出来报错是gb2312和utf-8不能编码网页中的某些字符.

单独使用编码也不能解决:
import codecs
print open("lsxk.txt").read().decode("utf-8")

可能说的不是很清楚`以上是我遇到的问题们,还请高手帮帮忙,不甚感谢!

BeautifulSoup 版本有问题,使用3.03就可以了

import urllib2
from BeautifulSoup import BeautifulSoup

f = urllib.urlopen('http://www.baidu.com')

html=f.read()
f.close()
soup = BeautifulSoup()
soup.feed(html)
print soup

BeautifulSoup() 不能直接 指定 编码

还有编码尽量 用GB18030 不要用GBK