Python 抓取網頁gb2312亂碼問題


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',r'wb')
wfile.write(RES)
wfile.close()
print RES
成功

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM