學生黨,苦逼的要作勤工儉學,天天都須要在學校的學生管理系統提交,苦逼的程序員表示不肯意天天都本身去點擊,果斷寫個腳本搞定之~javascript
抓取網站,模擬登陸等能夠參考連接:http://www.crifan.com/files/doc/docbook/web_scrape_emulate_login/release/html/web_scrape_emulate_login.htmlhtml
首先分析管理系統,抓包,分析java
首先抓取登陸程序員
能夠看到服務器使用session_id去認證的,這裏就須要post用戶名密碼,而後設置本地cookie就能夠了web
提交帶cookie數據包可參考連接:http://hi.baidu.com/zhuangzebo/item/da8ab9c13cea204aa8ba9457json
Session學習可參考:小程序
http://blogread.cn/it/article/4319服務器
這裏提交的data是字典形式的,須要注意編碼(就是由於這個編碼問題,搞了一天才OK)cookie
接着抓取提交報告的地方session
從這裏能夠看出,提交的時候須要帶上咱們上次提交的cookie
這裏提交的data是一個包含字典的列表,具體提交的時候須要注意格式,具體內容在程序中體現
源碼:(已經替換敏感字符)
1 #filename:post_name_list1.py 2 #-*-coding:UTF-8-*- 3 4 import urllib,urllib2,httplib,cookielib,sys,time 5 6 HttpClient1=None 7 HttpClient2=None 8 cj='' 9 10 #----------獲取當前文件系統的編碼--------- 11 type=sys.getfilesystemencoding() 12 data1={ 13 "LoginName" : "00000", 14 "Password" : "313212313132132132", 15 "UniversityCode" : "123131", 16 "LoginWay" : "Number" 17 } 18 19 #for key in data1: 20 # print "%s,%s"%(key,data1[key]) 21 22 #-------設置urllib2的Http功能對象--------- 23 cj=cookielib.CookieJar() 24 opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 25 urllib2.install_opener(opener) 26 27 params1 = urllib.urlencode({"data":"%s"%(data1)}) 28 headers = {"Content-type": "application/x-www-form-urlencoded", 29 "Accept": "application/json, text/javascript, */*; q=0.01", 30 "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0", 31 "Referer":" http://localhost/index.html", 32 "Connection":"keep-alive" 33 } 34 35 #----------這裏是提交post數據包的兩種方式,一個能夠帶cookie------- 36 37 #httpClient = httplib.HTTPConnection("localhost", 80) 38 #httpClient.request("POST", "/Rbac/Login", params1, headers) 39 40 HttpClient1=urllib2.Request("http://localhost/Rbac/Login",params1,headers=headers) 41 response1=urllib2.urlopen(HttpClient1) 42 #response = httpClient.getresponse() 43 #print response.status 44 #print response.reason 45 #print response1.read() 46 #print response.getheaders() #獲取頭信息 47 ''' 48 if response.getheader('Set-Cookie')!="": 49 cj=response.getheader('Set-Cookie').split(';')[0] 50 headers['Cookie']=cj 51 print response.getheader('Date').split(';') 52 print cj 53 else: 54 print "get no cookie" 55 ''' 56 57 print "當前cookie:" 58 for cookie in cj: 59 print cookie 60 61 ####列表中Note爲中文的時候會出錯,主要是由於下面urlencode函數%s的緣由,暫時沒找到辦法修改 62 ####可是當data2爲字典時就能夠,那個不須要%s輸出 63 data2=[ 64 {"Chief":{"Id":"000000000"},"Student":{"Id":"111111111"},"State":"0","Note":""}, 65 {"Chief":{"Id":"000000000"},"Student":{"Id":"222222222"},"State":"1","Note":"實習"}, 66 ] 67 #這裏date只是隨便寫個例子 68 #for x in data2: 69 # print x 70 date=time.strftime("%Y-%m-%d",time.localtime(time.time())) 71 print "提交日期:%s"%date 72 params2 = urllib.urlencode({"Data":data2,"Date":"%s"%date,"GenderCode":"0","ClassId":""}) 73 74 #httpClient = httplib.HTTPConnection("localhost", 80,timeout=30) 75 #httpClient.request("POST", "/Report/SubmitReport?uid=0000000&rid=312312312312321", params2, headers) 76 #response = httpClient.getresponse() 77 78 HttpClient2=urllib2.Request("http://localhost/Report/SubmitReport?uid=000000000&rid=1132132123132132",params2,headers=headers) 79 response2=urllib2.urlopen(HttpClient2) 80 81 #--------以當前系統的編碼方式輸出----------- 82 print "提交結果:" 83 print response2.read().decode('UTF-8').encode(type) 84 85 #----------關閉鏈接------------ 86 if (response1 or response2): 87 response1.close() 88 response2.close()
總結: