1. 程式人生 > >Python 抓取網頁gb2312亂碼問題

Python 抓取網頁gb2312亂碼問題

發現 file read earch () spa .com pycharm close

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  : [email protected]
# @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亂碼問題