1. 程式人生 > >python爬蟲獲取強智科技教務系統學科成績(模擬登入+成績獲取)

python爬蟲獲取強智科技教務系統學科成績(模擬登入+成績獲取)

直接貼出程式碼提供分享
歡迎訪問例項(本作者自己寫的網站):www.wjn1996.cn/estudy,進入首頁往下點選“常用工具》教務成績查詢”,網站採用jsp呼叫python指令碼,具體疑問可提出。


 
 
import urllib
import urllib.request
import urllib.parse
import http.cookiejar
PostUrl = "http://jwgl.just.edu.cn:8080/jsxsd/xk/LoginToXk"
cookie =  http.cookiejar.CookieJar()
hander = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(hander)
headers ={
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Connection':'keep-alive',
    'Accept-Language':'zh-CN,zh;q=0.8',
    'Content-Type':'application/x-www-form-urlencoded',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
}
PostData = {
    'USERNAME':'******',#'這裡填寫學號
    'PASSWORD':'******'#這裡填寫密碼
}
data = urllib.parse.urlencode(PostData).encode(encoding = 'utf-8')
request = urllib.request.Request(PostUrl,data,headers)
response = opener.open(request)
result = response.read().decode('utf-8')
#print(result)
res = opener.open('http://jwgl.just.edu.cn:8080/jsxsd/kscj/cjcx_list')
#print(res.read().decode('utf-8'))
from bs4 import BeautifulSoup
html_text = BeautifulSoup(res.read().decode('utf-8'),'html.parser')
td = html_text.select('td')
all_test_list = []
list = []
for i in td:    
    if i.text != '':
        list.append(i.text)
    else:
        if len(list)>0:
            all_test_list.append(list)
            list = []
        continue
    #print (i.text)
#print(all_test_list)
for i in all_test_list:
    print(i)#這裡輸出每一個課程的成績list
 #   print(i[3] , i[4])