python 登陆天津理工教务处 选课

来源:互联网 发布:帆盛点胶机编程 编辑:程序博客网 时间:2024/04/28 03:35

import urllibimport syss = "许莹莹"print urllib.quote(s.decode(sys.stdin.encoding).encode('utf8'))print urllib.quote(s.decode(sys.stdin.encoding).encode('gbk'))print urllib.unquote("%E8%AE%B8%E8%8E%B9%E8%8E%B9")print urllib.unquote("%D0%ED%D3%A8%D3%A8")
上面代码是用来utf8,gbk编码和解码的


代码如下以后再注释:

#!/usr/local/bin/python3.2# -*- coding: cp936 -*-import urllib2import urllibimport urlparseimport io,os,sysimport cookielibfrom xml.etree import ElementTree as ETimport sysreload(sys)sys.setdefaultencoding('utf8')def LogIn():    #cookies    cookies = urllib2.HTTPCookieProcessor()    opener = urllib2.build_opener(cookies)    urllib2.install_opener(opener)    postdata={"loginType":"2","username":LOGIN,"name":LOGIN,"jwydm":"","password":PASSWD,"pswd":PASSWD,"jwypswd":"","image.x":"27","image.y":"10"}     login = urllib2.urlopen(URL,urllib.urlencode(postdata))def xuanke(daima,banhao,mytype):    URL = "http://59.67.148.66/selection/xuanke/doing/xkround1.asp"    postdata ={"txtKcdm":daima,"txtJxbh":banhao,"ddlczlb":mytype,"Submit":"提交"}    #txtKcdm=0667066&txtJxbh=2&ddlczlb=1&Submit=提交    print urllib.urlencode(postdata)    print urllib.urlencode({"Submit":"提交"})    #try :    f = urllib2.Request(URL,urllib.urlencode(postdata))    req = urllib2.urlopen(URL,urllib.urlencode(postdata))    #print req    #f = urllib2.Request(URL)    #except :    #print "NO"            #mdir=sys.path[0]+'/'    #file = open(mdir+"tt.html",'w')    #file.write(f.read())    #file.close()    def ask(URL):    #web request    req=urllib2.Request(URL.strip())    f=urllib2.urlopen(URL)    s=f.read()# s=s.decode('utf8','ignore')    #print dir(f)    #print f.headers    mdir=sys.path[0]+'/'    file=open(mdir+'admin5.html','w')    file.write(s)    file.close()    print "hello"LOGIN = ""PASSWD =""cookiefile = './cookies.dat'URL = "http://59.67.148.66/index.asp"# cookieLogIn()#ask(URL)#ask("http://59.67.148.66/score/xsywcjcy.asp")Downphoto(2010)#myread()#xuanke("0667066","2","3")#ask("http://59.67.148.66/selection/xuanke/doing/xkround1.asp")#print "yes"def Downphoto(name):    beg = name * 10000+2741    end = beg + 9999    mdir=sys.path[0]+'/'    for i in range(beg+1,end):        temp = str(i)+".jpg"        print temp        try:            req = urllib2.Request("http://59.67.148.66/cet46/photo/apply/"+temp)            f =  urllib2.urlopen(req)            file = open(mdir+'/aa/'+temp,'wb')            file.write(f.read())            file.close()        except :            print str(i)+"不存在"def myread():    mdir=sys.path[0]+'/'    file = open(mdir+'admin5.html','r');    txt = file.read()    temp =""    begin = txt.find('<table')    end = txt.find('/table>')    txt = txt[begin:end]    file.close()    file = open(mdir+'admin5.html','w');    file.write(txt)    txt2 = '</tr>'    strlist = txt.split(txt2)   # print strlist    for i in range(1,len(strlist)):        temp = strlist[i]        ans = ""        str2 = temp.split('</td>')        t = 0        # print str2        for j in str2:            t += 1            wend = j.rfind('</font>')           # print j           # wbeg = j.rfind('>',wend)            p = j[:wend]            wbeg = p.rfind('>')            if (t == 1 or t == 3 or t == 6):                ans += " " + p[wbeg+1:].strip()        print ans    file.close()    


#!/usr/local/bin/python3.2# -*- coding: cp936 -*-import urllib2import urllibimport urlparseimport io,os,sysimport cookielibfrom xml.etree import ElementTree as ETimport sysreload(sys)sys.setdefaultencoding('utf8')  #设置默认的编码def LogIn():     #cookies  设置cookie 直接复制粘贴就可以了    cookies = urllib2.HTTPCookieProcessor()    opener = urllib2.build_opener(cookies)    urllib2.install_opener(opener)    #这边需要你自己去抓包早到post的的参数 一一对应就可以了 抓包可以利用chrome的F12    postdata={"loginType":"2","username":LOGIN,"name":LOGIN,"jwydm":"","password":PASSWD,"pswd":PASSWD,"jwypswd":"","image.x":"27","image.y":"10"}    #loginType=2&username=20101941&name=20101941&jwydm=&password=85592270&pswd=85592270&jwypswd=&image.x=27&image.y=10    login = urllib2.urlopen(URL,urllib.urlencode(postdata))  #进行表单编码然后 提交url urlopen 可以进行get 和post的提交    #urlopen(url, data=None)    #-- Basic usage is the same as original    #urllib.  pass the url and optionally data to post to an HTTP URL, and    #get a file-like object back.  One difference is that you can also pass    #a Request instance instead of URL.  Raises a URLError (subclass of    #IOError); for HTTP errors, raises an HTTPError, which can also be    #treated as a valid response.def xuanke():    #这个提交有httpheader 有用urllib2.urlopen(request)    files = open('in.txt',"r")    s = files.readline()    postdata={"xh":LOGIN,"xm":XM,"gnmkdm":gnmkdm}    geturl = URL + '?'+urllib.urlencode(postdata);    print geturl    headers ={     "Host"  :"116.11.32.36",    "User-Agent"    :"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0",    "Accept"    :"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",    "Accept-Language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",    "Accept-Encoding":"gzip, deflate",    "Connection":"keep-alive",    "Referer":"http://116.11.32.36/%28a4gwqn55g5vscn55phjhpm55%29/xf_xsyxxxk.aspx?xh="+"LOGIN""&xm="+XM+"&gnmkdm="+gnmkdm        }    postdata={        "__EVENTTARGET":"",        "__EVENTARGUMENT":"",        "__VIEWSTATE":s,        "ddl_ywyl":"",        "ddl_kcgs":"",        "ddl_sksj":"",        "ddl_xqbs":"1",        xuankedaima:"on",        "Button1":"+提+交+"            };    pp = urllib.urlencode(postdata)    xuankechenggong = 0;    while(xuankechenggong == 0):        try:            f = urllib2.Request(geturl,pp) #获取request            for k in headers:                f.add_header(k, headers[k]) #添加header            req = urllib2.urlopen(f)      #发送请求                        html = req.read()   #这边是对获取数据的判断            str = html[0:100]            index = str.find("alert")            print str            if index == -1:                xuankechenggong = 1;                print "success"            else:                print "try again"                        except:            print "error try again"    #判断是否选课成功了def xuanke(daima,banhao,mytype):    URL = "http://59.67.148.66/selection/xuanke/doing/xkround1.asp" #要提交的网页    postdata ={"txtKcdm":daima,"txtJxbh":banhao,"ddlczlb":mytype,"Submit":"提交"} #提交的参数    #txtKcdm=0667066&txtJxbh=2&ddlczlb=1&Submit=提交  我所抓取的包的数据    print urllib.urlencode(postdata)    print urllib.urlencode({"Submit":"提交"})    #try :    f = urllib2.Request(URL,urllib.urlencode(postdata))    req = urllib2.urlopen(URL,urllib.urlencode(postdata))    #print req    #f = urllib2.Request(URL)    #except :    #print "NO"        #mdir=sys.path[0]+'/'    #file = open(mdir+"tt.html",'w')    #file.write(f.read())    #file.close()        def ask(URL):    #web request    req=urllib2.Request(URL.strip())    f=urllib2.urlopen(URL)    s=f.read()# s=s.decode('utf8','ignore')    #print dir(f)    #print f.headers    mdir=sys.path[0]+'/'    file=open(mdir+'admin5.html','w')    file.write(s)    file.close()    print "hello"LOGIN = "20101941"PASSWD ="85592270"cookiefile = './cookies.dat'URL = "http://59.67.148.66/index.asp"# cookieLogIn()#ask(URL)#ask("http://59.67.148.66/score/xsywcjcy.asp")Downphoto(2010)#myread()#xuanke("0667066","2","3")#ask("http://59.67.148.66/selection/xuanke/doing/xkround1.asp")#print "yes"def Downphoto(name):  #当时写的用来下载教务处图片的    beg = name * 10000+2741    end = beg + 9999    mdir=sys.path[0]+'/'    for i in range(beg+1,end):        temp = str(i)+".jpg"        print temp        try:            req = urllib2.Request("http://59.67.148.66/cet46/photo/apply/"+temp)            f =  urllib2.urlopen(req)            file = open(mdir+'/aa/'+temp,'wb')            file.write(f.read())            file.close()        except :            print str(i)+"不存在"def myread():    mdir=sys.path[0]+'/'    file = open(mdir+'admin5.html','r');    txt = file.read()    temp =""    begin = txt.find('<table')    end = txt.find('/table>')    txt = txt[begin:end]    file.close()    file = open(mdir+'admin5.html','w');    file.write(txt)    txt2 = '</tr>'    strlist = txt.split(txt2)   # print strlist    for i in range(1,len(strlist)):        temp = strlist[i]        ans = ""        str2 = temp.split('</td>')        t = 0        # print str2        for j in str2:            t += 1            wend = j.rfind('</font>')           # print j           # wbeg = j.rfind('>',wend)            p = j[:wend]            wbeg = p.rfind('>')            if (t == 1 or t == 3 or t == 6):                ans += " " + p[wbeg+1:].strip()        print ans    file.close()



原创粉丝点击