import urllib import urllib2 import cookielib filename = 'cookie.txt' #聲明一個MozillaCookieJar對象實例來保存cookie,以後寫入文件 cookie = cookielib.MozillaCookieJar(filename) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) #設置請求參數 values = {} values['__VIEWSTATE'] = "******" values['tbxUserID'] = "XXXXXXX" values['InputPwd'] = "XXXXXX" values['btnLogin.x'] = "55" values['btnLogin.y'] = "23" postdata = urllib.urlencode(values) #登陸教務系統的URL loginUrl = 'http://yjsgl.***.edu.cn/login.aspx' #模擬登陸,並把cookie保存到變量 #設置header user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36' opener.addheaders.append( ('Host', 'yjsgl.fzu.edu.cn') ) opener.addheaders.append( ('User-Agent', user_agent) ) opener.addheaders.append( ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8') ) opener.addheaders.append( ('Accept-Language', 'zh-CN,zh;q=0.8') ) opener.addheaders.append( ('Accept-Encoding', 'gzip, deflate') ) opener.addheaders.append( ('Connection', 'keep-alive') ) opener.addheaders.append( ('Referer', 'http://yjsgl.fzu.edu.cn/login.aspx') ) try: result = opener.open(loginUrl,postdata) except opener.URLError, e: print e.reason for item in cookie: print 'Name = '+item.name print 'Value = '+item.value #保存cookie到cookie.txt中 cookie.save(ignore_discard=True, ignore_expires=True) #利用cookie請求訪問另外一個網址,此網址是成績查詢網址 gradeUrl = 'http://yjsgl.***.edu.cn/xsgl/xsxx_show.aspx' #請求訪問成績查詢網址 result = opener.open(gradeUrl) print result.read()
# -*- coding: utf-8 -*- """ Created on Fri Apr 14 16:12:55 2017 @author: zeze """ import cookielib import urllib2 #建立MozillaCookieJar實例對象 cookie = cookielib.MozillaCookieJar() #從文件中讀取cookie內容到變量 cookie.load('cookie.txt', ignore_discard=True, ignore_expires=True) #建立請求的request Url = urllib2.Request("http://yjsgl.***.edu.cn/xsgl/xsxx_show.aspx") #利用urllib2的build_opener方法建立一個opener opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) response = opener.open(Url) print response.read()
import urllib #import cookielib import http.cookiejar filename = 'cookie.txt' # 聲明一個MozillaCookieJar對象實例來保存cookie,以後寫入文件 #設置一個cookie處理器,它負責從服務器下載cookie到本地,而且在發送請求時帶上本地的cookie cookie = http.cookiejar.MozillaCookieJar(filename) #opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) cookie_support = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(cookie_support, urllib.request.HTTPHandler) urllib.request.install_opener(opener) # 設置請求參數 values = {} values['__VIEWSTATE'] = "/wEPDwUJODA3MTI1345b2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAgUIYnRuTG9naW4FDEltYWdlQnV0dG9uMQ==" values['tbxUserID'] = "1345" values['InputPwd'] = "5345e" values['btnLogin.x'] = "55" values['btnLogin.y'] = "23" #postdata = urllib.urlencode(values) postdata=urllib.parse.urlencode(values).encode('utf-8') # 登陸教務系統的URL loginUrl = 'http://yjsgl.123.e34u.cn/login.aspx' # 模擬登陸,並把cookie保存到變量 # 設置header user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36' opener.addheaders.append(('Host', 'yjsgl.fzu.edu.cn')) opener.addheaders.append(('User-Agent', user_agent)) opener.addheaders.append( ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')) opener.addheaders.append(('Accept-Language', 'zh-CN,zh;q=0.8')) opener.addheaders.append(('Accept-Encoding', 'gzip, deflate')) opener.addheaders.append(('Connection', 'keep-alive')) opener.addheaders.append(('Referer', 'http://yjsgl.f234u.cn/login.aspx')) try: result = opener.open(loginUrl, postdata) except opener.URLError as e: print(e.reason) for item in cookie: print('Name = ' + item.name) print('Value = ' + item.value) # 保存cookie到cookie.txt中 cookie.save(ignore_discard=True, ignore_expires=True) # 利用cookie請求訪問另外一個網址,此網址是成績查詢網址 gradeUrl = 'http://yjsgl.fzu.edu.cn/xsgl/xsxx_show.aspx' # 請求訪問成績查詢網址 result = opener.open(gradeUrl) print("返回的結果") print(result.read().decode('utf8'))#先看.info 解決亂碼的問題 text = result.read() save_path="D:/snatch2.txt" # save_path 's file unnecessary to be exist f_obj = open(save_path,'wb') f_obj.write(text) print("snatch successfully.")