今天在ubuntu 上用python 采集产品信息,采集了 5个小时,完成了15000条信息的保存(html文件形式),跟着用python 写脚本导入数据库,发现打开的html中文乱码:
python
#coding=utf-8 import os import MySQLdb path="./os/" arr=os.listdir(path) html=open(path+arr[0],"r").read() print html
发现乱码 后来
然后用vim的
set fileencodeing查看html的编码 发现时latin1的编码格式
尝试
print html.decode("latin1").encode("utf-8")
发现还是乱码
奇怪了
后来花了几个小时时间查资料没结果
后来鬼使神差的用
print html.decode("gb2312").encode("utf-8")
发现正常了
后来发现 采集的网站 charset=gb2312”/>
希望对大家有帮助