Using Python to automate the course of the Dean's Office

Source: Internet
Author: User

First analyze the Web page, find the Academic Office login Verification Code

Then use Python to download the verification code directly to the local (the entire program through the Requests library implementation):

def GetRandCode():    =r‘http://jwxt.wust.edu.cn/whkjdx/verifycode.servlet‘    = foo.get(url)    withopen(‘randcode.jpg‘‘wb‘asfile:        file.write(ans.content)

After we find the verification code and continue to find the login API, we can find that the Web page sent a POST request, as well as the relevant parameters:

Then to implement the login is very simple, I first wrote a simple login implementation:

Foo=Foo=Requests.session () headers={"User-agent":"mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/49.0.2623.110 safari/537.36 ",}defLogin (username, password, randcode): URL= R ' Http://jwxt.wust.edu.cn/whkjdx/Logon.do?method=logon 'Information={' USERNAME ': Username,' PASSWORD ': Password,' Randomcode ': Randcode} ans=Foo.post (URL, data=Information, headers=Headers) Ans.raise_for_status () ans.encoding=Ans.apparent_encodingifAns.text.find (R ' http://jwxt.wust.edu.cn/whkjdx/framework/main.jsp ')!= -1:return True    Else:return False

Test the discovery can be normal login, and then to do to obtain a list of selected courses, the same way.
I found the address of this semester to get the list of courses is http://jwxt.wust.edu.cn/whkjdx/xkglAction.do?method=toFindxskxkclb&xnxq01id=2017-2018-2&zzdxklbname=1&type=1&jx02kczid=null very obvious parameter xnxq01id should be the semester number, the rules are easy to find. Then I implemented it directly in Python and found that the Web page returned a message with no access rights. Go back and analyze the login process, find an API for SSO (Point-to-point login), and try a new login function:

defLogin (username, password, randcode): URL= R ' Http://jwxt.wust.edu.cn/whkjdx/Logon.do?method=logon 'Ssourl= R ' Http://jwxt.wust.edu.cn/whkjdx/Logon.do?method=logonBySSO 'Information={' USERNAME ': Username,' PASSWORD ': Password,' Randomcode ': Randcode} ans=Foo.post (URL, data=Information, headers=Headers) Ans.raise_for_status () ans.encoding=Ans.apparent_encoding Ans2=Foo.post (Ssourl, headers) ans2.raise_for_status ()ifAns.text.find (R ' http://jwxt.wust.edu.cn/whkjdx/framework/main.jsp ')!= -1:return True    Else:return False

With the new login function, you can get a list of elective courses normally. The specific implementation is as follows:

defGetcourseslist (): URL= R ' http://jwxt.wust.edu.cn/whkjdx/xkglaction.do?method=tofindxskxkclb&xnxq01id=2017-2018-2& Zzdxklbname=1&type=1&jx02kczid=null 'Ans=Foo.get (URL, headers=Headers) Ans.raise_for_status () ans.encoding=Ans.apparent_encoding courseslist=Re.findall (R ' <td height= "style=" text-overflow:ellipsis; white-space:nowrap; overflow:hidden; width= "\d+" title= " . *" ', Ans.text) xkljlist=Re.findall ("Javascript:vjsmod\ (\ '.*\ '", Ans.text) KeyName=[' KCMC ',' KKDW ',' Zyfx ',' XF ',' Yxrs ',' YL ',' Skjs ',' Skzc ',' SKSJ ',' SKDD ',' KCSX ',' Kcxz ',' Fzm ',' Xbyq '] Result=[] Item={} bar= 0Index= 0     forIinchCourseslist:left=I.find (R ' title= "') Right=I[left+ 7:].find (R ' "') text=I[left+ 7: Left+Right+ 7]#print (i)        #print (text)Item[keyname[bar]]=Text Bar=Bar+ 1        if(Bar==  -): Left=Xkljlist[index].find ("'") Right=Xkljlist[index][left+ 1:].find ("'") text=Xkljlist[index][left+ 1: Left+Right+ 1] Item[' Xklj ']=Text Index=Index+ 1Result.append (item) Item={} bar= 0    returnResult

Where the regular expression matches all the information in the Web page list, each 14 items is a course of all the information, the specific information corresponding to which, you can look at the table header, I use a dictionary to save the information of these courses, and then saved to a list, each message of the phonetic shorthand is the dictionary of the corresponding key name, and then there is a xkljthe link used to save the selected lesson needs to be get accessed, so that the choice of course is very simple, only need to send a request to this link get :

def ChoseCourseByLink(link):    =‘http://jwxt.wust.edu.cn‘+ link    == headers)    ans.raise_for_status()    = ans.apparent_encoding    return ans.text

Then use the same method to construct the credit-system list of elective courses:

defGetCoursesList2 (): URL= R ' http://jwxt.wust.edu.cn/whkjdx/xkglaction.do?method=tofindxskxkclb&xnxq01id=2017-2018-2& Zzdxklbname=6&type=1&jx02kczid=null 'Ans=Foo.get (URL, headers=Headers) Ans.raise_for_status () ans.encoding=Ans.apparent_encoding courseslist=Re.findall (R ' <td height= "style=" text-overflow:ellipsis; white-space:nowrap; overflow:hidden; width= "\d+" title= " . *" ', Ans.text) xkljlist=Re.findall ("Javascript:vjsmod\ (\ '.*\ '", Ans.text) KeyName=[' KCMC ',' KKDW ',' Zyfx ',' XF ',' Yxrs ',' YL ',' Skjs ',' Skzc ',' SKSJ ',' SKDD ',' KCSX ',' Kcxz ',' Fzm ',' Xbyq '] Result=[] Item={} bar= 0Index= 0     forIinchCourseslist:left=I.find (R ' title= "') Right=I[left+ 7:].find (R ' "') text=I[left+ 7: Left+Right+ 7]#print (i)        #print (text)Item[keyname[bar]]=Text Bar=Bar+ 1        if(Bar==  -): Left=Xkljlist[index].find ("'") Right=Xkljlist[index][left+ 1:].find ("'") text=Xkljlist[index][left+ 1: Left+Right+ 1] Item[' Xklj ']=Text Index=Index+ 1Result.append (item) Item={} bar= 0    returnResult

Such a simple library of lessons to be achieved, the time to grab a lesson only need to call the relevant interface on the line, and finally all the code on my Github: https://github.com/Rugel/wustjwxt

Using Python to automate the course of teaching at the Hkust academic Office

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.