码迷,mamicode.com
首页 > 编程语言 > 详细

Python 抓取网页gb2312乱码问题

时间:2018-04-06 10:49:24      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:chardet   gpo   gb2312   put   ada   test   utf8   env   sof   

python 爬取学校所有人四六级成绩时发现爬出网页中文乱码

遂google

得到一解决方案

# -*- coding:utf8 -*-  
  
import urllib2  
  
req = urllib2.Request("http://jwgl.hist.edu.cn/jwweb/jiaow/data46/search1.asp")
res = urllib2.urlopen(req) 
html
= res.read() res.close() html = unicode(html, "gb2312").encode("utf8") #gb2312--->utf-8

print html

但这并没有解决问题  

开始继续试错

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018-04-05 21:59
# @Author  : wqjhky@gmial.com
# @File    : Test2.py
# @Software: PyCharm
import urllib2
import urllib
import sys
import chardet
url = "http://jwgl.hist.edu.cn/jwweb/jiaow/data46/search1.asp "
key = raw_input("请输入学号")
formadate = {
    "ksh1":key,
    "Submit":"%C8%B7%B6%A8"
}
data = urllib.urlencode(formadate)
request = urllib2.Request(url,data=data)
RES  = urllib2.urlopen(request).read()
RES = RES.decode(gb2312).encode(utf-8)
wfile=open(r./1.html,rwb)
wfile.write(RES)
wfile.close()
print RES
成功

 

Python 抓取网页gb2312乱码问题

标签:chardet   gpo   gb2312   put   ada   test   utf8   env   sof   

原文地址:https://www.cnblogs.com/wqja/p/8726827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!