g******e 发帖数: 352 | 1 试着用python urlopen 获取mitbbs的页面
Windows下,没有再加encode, decode
遇到一个奇怪的问题,有些帖子的页面抓下来没问题,
但有些帖子的页面获取到的就是乱码,如果用chardet来检测,也检测不出
是什么编码 (返回None). 而那些能正常抓下来的页面,则chardet会正确返回
gb2312
如果加上content.encode('gb2312').encode(type)
就报错'gb2312' codec can't decode bytes in position 1-2: illegal multibyte
sequence
mitbbs所有页面应该都是gb2312编码呀,
有哪位大牛给看看问题出在哪里,谢谢,有包子答谢 |
r****t 发帖数: 10904 | 2 贴个有问题的例子出来,一般用 regex massage 一下就行了。 |
g******e 发帖数: 352 | 3 code在这里,谢谢
import urllib
import urllib2
import sys
import chardet
response = urllib.urlopen('http://www.mitbbs.com/article_t/Programming/31190605.html')
content = response.read()
print content
chardet.detect(content)
type = sys.getfilesystemencoding()
print content.decode('gb2312').encode(type)
【在 r****t 的大作中提到】 : 贴个有问题的例子出来,一般用 regex massage 一下就行了。
|
d****e 发帖数: 251 | 4 Check out FAQ: http://chardet.feedparser.org/docs/faq.html
You should first respect the explicit encoding.The auto detection is
inaccurate and non-standard.
【在 g******e 的大作中提到】 : 试着用python urlopen 获取mitbbs的页面 : Windows下,没有再加encode, decode : 遇到一个奇怪的问题,有些帖子的页面抓下来没问题, : 但有些帖子的页面获取到的就是乱码,如果用chardet来检测,也检测不出 : 是什么编码 (返回None). 而那些能正常抓下来的页面,则chardet会正确返回 : gb2312 : 如果加上content.encode('gb2312').encode(type) : 就报错'gb2312' codec can't decode bytes in position 1-2: illegal multibyte : sequence : mitbbs所有页面应该都是gb2312编码呀,
|
g******e 发帖数: 352 | 5 谢谢您的回复,问题其实不是在chardet上,
就算不用chardet,同样的python code, 我用urlopen抓下来的一大部分mitbbs网页就是
乱码,根本print不出来,保存到文件也是乱码,试图用gb2312解码也报错。
但是有一小部分mitbbs网页能正确print出来
想不出问题出在哪里,我的环境是python 2.6, windows xp 中文版
如果方便的话,哪位大侠可以在机器上run一下这几行
简单的code? 能正确print吗?
import urllib
import urllib2
import sys
response = urllib.urlopen('http://www.mitbbs.com/article_t/Programming/31190605.html')
content = response.read()
print content
type = sys.getfilesystemencoding()
print content.decode('gb2312').encode(type)
【在 d****e 的大作中提到】 : Check out FAQ: http://chardet.feedparser.org/docs/faq.html : You should first respect the explicit encoding.The auto detection is : inaccurate and non-standard.
|
d****e 发帖数: 251 | 6 gzipped (maybe some of them based on your experience), you may use the gzip
module. You know what to do from there.
Good luck. |
g******e 发帖数: 352 | 7 问题解决了,谢谢大牛,包子奉上
gzip
【在 d****e 的大作中提到】 : gzipped (maybe some of them based on your experience), you may use the gzip : module. You know what to do from there. : Good luck.
|