-
-
Save zhangzhiqiangcs/d1cf68492bde10d46122bfe55fb02c92 to your computer and use it in GitHub Desktop.
zzu远程教育刷题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'zhangzhiqiang' | |
import requests | |
import re | |
from collections import defaultdict | |
from pprint import pprint | |
from urllib.parse import quote, urlencode, urlparse, urlunparse | |
from bs4 import BeautifulSoup as bs | |
login_url = "http://vls3.zzu.edu.cn/vls2s/zzjlogin.dll/login" | |
logout_url = None | |
uid = "17119777076" | |
pw = "19910213" | |
param = {'uid': uid, 'pw': pw} | |
frameList = list() | |
subjectTestURL = list() | |
chapterTestUrl = list() | |
s = None | |
def logout(): | |
pass | |
def create_bs(content): | |
return bs(content, 'html5lib') | |
def login(): | |
global login_url, param, s | |
#print(login_url, param) | |
s = requests.Session() | |
r = s.post(login_url, data=param) | |
if r.status_code != requests.codes.ok: | |
print('login error') | |
sys.exit() | |
else: | |
return r | |
def goto_main(response): | |
global s | |
soup = create_bs(response.content) | |
url = soup.find_all('input')[0].get('onclick')[17:-14] | |
r = s.get(url) | |
if r.status_code == requests.codes.ok: | |
return r | |
else: | |
print('error') | |
sys.exit(0) | |
def getFramePages(response): | |
global frameList | |
soup = create_bs(response.content) | |
url = list() | |
for i in range(3): | |
url.append(soup.find_all('frame')[i].get('src')) | |
for ul in url: | |
r = s.get(ul) | |
frameList.append(create_bs(r.content)) | |
# print(create_bs(r.content).prettify()) | |
def gotoStudy(frameSoup): | |
global subjectTestURL, s | |
vpages = dict() | |
cur_term = frameList[2].find_all(text=re.compile("本学期"))[3] | |
for t in cur_term.next_siblings: | |
if t.name == 'a': | |
vpages[t.get_text()] = t.get('href') | |
if len(vpages) == 0: | |
print(' error ') | |
sys.exit(0) | |
for t, v in vpages.items(): | |
# print(t) | |
# print(v) | |
print('togo learn ', t) | |
r = s.get(v) | |
soup = create_bs(r.content) | |
# print(soup.prettify()) | |
fd = soup.find_all(text=re.compile("在线测试")) | |
sp = fd[1].parent.parent | |
if(len(sp.find_all(text=re.compile('不提供在线测试'))) == 0): | |
# print(t, sp.get['src']) | |
# print(sp) | |
print(sp.get('href')) | |
subjectTestURL.append(sp.get('href')) | |
else: | |
print(t, '没有在线测试') | |
# for d in fd: | |
# print(d.parent.parent) | |
# url = soup.find_all(text=re.compile("在线测试"))[1].parent.get('href') | |
# print(url) | |
# print(soup.prettify()) | |
# url = soup.find_all(text=re.compile("课件学习"))[0].parent.parent.get('href') | |
def genChapterTestUrl(): | |
global chapterTestUrl, s | |
temp = list() | |
for url in subjectTestURL: | |
r = s.get(url) | |
# pprint(vars(r)) | |
soup = create_bs(r.content) | |
# print(soup.find_all('meta')[1].get('content')) | |
fresh_url = soup.find_all('meta')[1].get('content')[7:-1] | |
# print(fresh_url) | |
r = s.get(fresh_url) | |
soup = create_bs(r.content) | |
# print(soup.prettify()) | |
result = soup.find_all(text=re.compile('开始在线测试')) | |
for t in result: | |
p = t.parent.parent | |
# print(p.get('href')) | |
chapterTestUrl.append(p.get('href')) | |
# continue | |
# o = urlparse(fresh_url) | |
# print(o) | |
# ry = quote(o.query, encoding='gb2312') | |
# uu = o.scheme + '://' + o.netloc + o.path + '?' + ry ; | |
# fresh_url_1 = urlunparse(o) | |
# continue | |
# temp.append(soup.find_all('meta')[1].get('content')[7:-1]) | |
# soup = create_bs(r.content) | |
# print(soup.prettify()) | |
# lt = fresh_url_1.strip('http://123.15.57.74/vls2s/vls3isapi.dll/myviewdatalist?ptopid=') | |
# url_encoded = quote(lt, encoding='gb2312') | |
# url_encoded = 'http://123.15.57.74/vls2s/vls3isapi.dll/myviewdatalist?ptopid=' + url_encoded | |
# print(url_encoded) | |
# print(uu) | |
# r = s.get(uu) | |
# print(temp) | |
# for url in temp: | |
# lt = url.strip('http://') | |
# url_encoded = quote(lt) | |
# # url_encoded = urlencode(lt) | |
# url_encoded = 'http://' + url_encoded | |
# print(url_encoded) | |
# r = s.get(url_encoded, headers={'Accept-Encoding': 'deflate, gzip'}) | |
# soup = create_bs(r.content) | |
# print(soup.prettify()) | |
def do_exe(): | |
print('charpterTestUrl lenght', len(chapterTestUrl)) | |
for url in chapterTestUrl[5:6]: | |
r = s.get(url) | |
soup = create_bs(r.content) | |
# print(soup.prettify()) | |
ipt = soup.find_all('form') | |
form = ipt[0] | |
action = form.get('action') | |
param = dict() | |
for ip in form.find_all('input'): | |
# print(ip.get('name'), ip.get('value')) | |
param[ip.get('name')] = ip.get('value') | |
r = s.post(action, data=param) | |
sp = create_bs(r.content) | |
form = sp.find_all('form')[0] | |
action = form.get('action') | |
method = form.get('method') | |
print(action) | |
print(method) | |
# every paper | |
itmDan = defaultdict(list) | |
itmDuo = defaultdict(list) | |
for ip in form.find_all('input'): | |
print(ip) | |
if ip.get('type') == 'button' or ip.get('type') == 'submit': | |
continue | |
iptname = ip.get('name') | |
iptvalue = ip.get('value') | |
# if iptname[-1] >= 'A' and iptname[-1] <= 'D': | |
# iptname = iptname[0:-1] | |
# itmDuo[iptname].append(iptvalue) | |
# continue | |
itmDan[iptname].append(iptvalue) | |
print(itmDan.items()) | |
jiaoJuan(action, itmDan) | |
# print(itmDuo.items()) | |
# print(sp.prettify()) | |
def jiaoJuan(action, allParam): | |
param = dict() | |
param.update({ | |
'paperid': allParam['paperid'], | |
'ptopid': allParam['ptopid'] | |
}) | |
r = s.post(action, data=param) | |
sp = create_bs(r.content) | |
print(sp.prettify()) | |
def test(): | |
r = login() | |
r = goto_main(r) | |
getFramePages(r) | |
gotoStudy(frameList[2]) | |
genChapterTestUrl() | |
do_exe() | |
if __name__ == '__main__': | |
login() | |
''' | |
http://171.8.225.131/vls2s/vls3isapi.dll/logout?ptopid=CC307AF2375E4455826DB3EBA1411CD5 | |
http://123.15.57.74/vls2s/vls3isapi.dll/mygetonetest?ptopid=B35F89DCE1A147AEB42F1452646A7632&ruid=211649207161&keid=0005 | |
http://123.15.57.74/vls2s/vls3isapi.dll/mygetonetest?ptopid=B35F89DCE1A147AEB42F1452646A7632&ruid=211651458713&keid=9087 | |
<span style="font-size: 14px"> | |
恭喜,交卷操作成功完成!你本次进行的《高级语言程序设计》第05章在线测试的得分为 | |
<font color="#FF0000"> | |
<span style="font-weight: 700; background-color: #FFFF00">3分</span> | |
</font> | |
(满分20分),因未超过库中记录的成绩19分,本次成绩未入库。若对成绩不满意,可重新再测,取最高分。<br> | |
<ul>测试结果如下: | |
<li><font color="#FF0000">1.1 [单选] [错] 设x和y均为int型变量,则以下语句:x=x+y; y=x-y; x=x-y; 的功能是:</font></li> | |
<li><font color="#FF0000">1.2 [单选] [错] 已有int x=2,y=7;则printf("%d",x+y==6);的输出结果是:</font></li> | |
<li><font color="#FF0000">1.3 [单选] [错] 设int x=10,y=3,则语句 printf(“%d%d\n”,x+y,x-y);的输出结果是():</font></li> | |
<li><font color="#FF0000">1.4 [单选] [错] 下列算术运算符中,只能在两个整型数据之间进行的运算符是( )。</font></li> | |
<li><font color="#FF0000">1.5 [单选] [错] 函数putchar可以向终端输出一个( )。</font></li> | |
<li><font color="#008000">2.1 [多选] [对] 以下合法的表达式是:</font></li> | |
<li><font color="#FF0000">2.2 [多选] [错] x、y、z被定义为int 型常量,若从键盘给 x、y、z输入数据,不正确的输入语句是:</font></li> | |
<li><font color="#FF0000">2.3 [多选] [错] 已知“int a, b; scanf ( "%d%d", &a, &b );”。若为a和b输入值,可以作为输入分隔符的是( )。</font></li> | |
<li><font color="#FF0000">2.4 [多选] [错] 下列属于C语言标准库函数的为()。</font></li> | |
<li><font color="#FF0000">2.5 [多选] [错] 已知“int a=1;char b=65; float c=2.0;double d=3.5;”。则下述赋值表达式结果为整型的是()。</font></li> | |
<li><font color="#FF0000">3.1 [判断] [错] 用户标识符中可以出现下划线,但不可以放在用户标识符的开头。</font> | |
</li><li><font color="#FF0000">3.2 [判断] [错] 逻辑关系“a≥10或a≤0”正确表示的C语言表达式是:a>=10 or a<=0 。</font></li> | |
<li><font color="#008000">3.3 [判断] [对] 算术运算符的结合方向均为自左至右。</font></li> | |
<li><font color="#FF0000">3.4 [判断] [错] 赋值运算符的优先级低于所有的算术运算符。</font></li> | |
<li><font color="#FF0000">3.5 [判断] [错] C语言表达式值的类型可以根据环境来确定。</font></li> | |
</ul> | |
</span></font> | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment