爬学校教务处的成绩单

来源:互联网 发布:电梯群控算法 编辑:程序博客网 时间:2024/04/28 09:57

参考——Python爬虫教程

本文章是很久之前创作的,最近发现有很多人在看,重新编辑一下,解释一下部分代码吧。

本代码实现的具体功能,是通过python来模仿登陆 ‘电子科技大学’ 的教务系统,然后爬取成绩单。

如果是电子科技大学的同学们,需要改的地方不多:

1. 查看字典变量‘postdata_raw’是否需要更新(因为我完成的日期是2016年4月4日,代码里面有注释,可能网站有更改)

2. 将username 和 password 这两个key赋值

如果你想改为自己学校的教务处,可模仿该代码,但需要自行改正如下部分:

1. 所有的‘*url’变量改成你想爬取得网站,同时还要更改大部分的网页处理方式;

2. postdata_raw 这个字典变量可能需要新增或者删除某些key;

3. 输入username 和 password。

声明:由于每个教务处的系统不同,以及电子科大的教务处也有很大程度的改变,大家不可完全照抄代码,还是应该先理解原理,再写代码。原理部分,可以看上面给出的参考

参考——Python爬虫教程。


# this is a python program for scrawl the all semester grade in UESTC# please filing the username and the password# finish date: April 4th, 2016import urllibimport urllib2import cookielibfrom bs4 import BeautifulSoupimport lxmlfilename = 'cookie.txt'grade_htmlfile = 'grade_all.html'# all_gradefilename = 'grade_all.txt'# all_gradefile = open(all_gradefilename, 'w')grade_html_open = open(grade_htmlfile, 'w')cookie = cookielib.MozillaCookieJar(filename)opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))postdata_raw = {'username':'',# filing the blank of your username'password':'',# filing the blank of your password'dllt':'userNamePasswordLogin','_eventId':'submit','rmShown':'1','lt': ' ','execution': ' ',}# print postdata, type(postdata)loginUrl = 'http://idas.uestc.edu.cn/authserver/login?service=http://portal.uestc.edu.cn/index.portal'first_result = opener.open(loginUrl)# first, get the lt and execution of the HTMLhtml = first_result.read()soup = BeautifulSoup(html, 'lxml')lt_string = str(soup.find_all('input')[2])# print soup.find_all('input')lt_value_raw = lt_string.split('=')[3]# print lt_value_rawlt_value = lt_value_raw.strip("\"").strip("\"/>")postdata_raw['lt'] = lt_value# print lt_valueexecu_string = str(soup.find_all('input')[4])execu_value_raw = execu_string.split('=')[-1]execu_value = execu_value_raw.strip("\"").strip("\"/>")postdata_raw['execution'] = execu_valuepostdata = urllib.urlencode(postdata_raw)# print postdataloginUrl_login = 'http://idas.uestc.edu.cn/authserver/login' # the url for postdataloginUrl_success = loginUrl+ '&' + postdataprint loginUrl_successsuccess_login_result = opener.open(loginUrl_success)# second_urllib2_result = urllib2.urlopen(loginUrl_success)# success_second_urllib2_html = second_urllib2_result.read()# success_second_html = second_result.read()# third_result = opener.open("http://portal.uestc.edu.cn/index.portal")# success_third_html = third_result.read()grade_all_url = 'http://eams.uestc.edu.cn/eams/teach/grade/course/person!historyCourseGrade.action?projectType=MAJOR'grade_all_result = opener.open(grade_all_url)grade_all_html = grade_all_result.read() grade_html_open.write(grade_all_html)grade_html_open.flush()grade_html_open.close()# post_result = opener.open()# print excu_value# print html# cookie.save(ignore_discard=True, ignore_expires=True)# gradeUrl = 'http://jwxt.sdu.edu.cn:7890/pls/wwwbks/bkscjcx.curscopre'# result = opener.open(gradeUrl)# print result.read()

0 0