|
|
@ -10,7 +10,9 @@
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
#导入所需模块
|
|
|
|
#导入所需模块
|
|
|
|
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
|
|
|
|
import time
|
|
|
|
from selenium.webdriver import Chrome
|
|
|
|
from selenium.webdriver import Chrome
|
|
|
|
from selenium.webdriver.chrome.options import Options
|
|
|
|
from selenium.webdriver.chrome.options import Options
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
@ -19,10 +21,11 @@ import re
|
|
|
|
from lxml import etree
|
|
|
|
from lxml import etree
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
import requests
|
|
|
|
import requests
|
|
|
|
from cloud import is_exist,download,upload
|
|
|
|
from cloud import is_exist,download
|
|
|
|
#配置参数
|
|
|
|
#配置参数
|
|
|
|
opt = Options()
|
|
|
|
opt = Options()
|
|
|
|
opt.add_experimental_option('detach', True)
|
|
|
|
opt.add_experimental_option('detach', True)
|
|
|
|
|
|
|
|
opt.add_argument('--headless')
|
|
|
|
chrome_driver = 'D:\ChromeDownload\chromedriver-win64\chromedriver-win64'
|
|
|
|
chrome_driver = 'D:\ChromeDownload\chromedriver-win64\chromedriver-win64'
|
|
|
|
#以下部分在发行版本需要优化UI
|
|
|
|
#以下部分在发行版本需要优化UI
|
|
|
|
# url = input('请输入作业中任意一关的网址:')
|
|
|
|
# url = input('请输入作业中任意一关的网址:')
|
|
|
@ -33,7 +36,7 @@ user_name = 'hnu202311020126'
|
|
|
|
password = 'hzy123456'
|
|
|
|
password = 'hzy123456'
|
|
|
|
# 另外,目前好像只有实训作业有这些参数,其他的作业例如编程作业就没有,所以先判断一下是否为实训作业,可以通过用户输入的url判断
|
|
|
|
# 另外,目前好像只有实训作业有这些参数,其他的作业例如编程作业就没有,所以先判断一下是否为实训作业,可以通过用户输入的url判断
|
|
|
|
# 主要是看educoder.net/后面是否有tasks,如果有,则是实训作业,否则,不是实训作业
|
|
|
|
# 主要是看educoder.net/后面是否有tasks,如果有,则是实训作业,否则,不是实训作业
|
|
|
|
def is_practice(url):
|
|
|
|
def is_practice(url:str) -> bool:
|
|
|
|
obj=re.compile(r'www.educoder.net/tasks')
|
|
|
|
obj=re.compile(r'www.educoder.net/tasks')
|
|
|
|
if obj.search(url):
|
|
|
|
if obj.search(url):
|
|
|
|
return True
|
|
|
|
return True
|
|
|
@ -41,7 +44,7 @@ def is_practice(url):
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
if is_practice(url):
|
|
|
|
if is_practice(url):
|
|
|
|
#构造selenium对象
|
|
|
|
#构造selenium对象
|
|
|
|
safari = Chrome()
|
|
|
|
safari = Chrome(options=opt)
|
|
|
|
safari.get(url)
|
|
|
|
safari.get(url)
|
|
|
|
#模拟登录
|
|
|
|
#模拟登录
|
|
|
|
safari.implicitly_wait(10)
|
|
|
|
safari.implicitly_wait(10)
|
|
|
@ -65,14 +68,24 @@ if is_practice(url):
|
|
|
|
response = requests.get(url=id_url, headers=headers)
|
|
|
|
response = requests.get(url=id_url, headers=headers)
|
|
|
|
shixun_id = dict(response.json())['challenge']['shixun_id']
|
|
|
|
shixun_id = dict(response.json())['challenge']['shixun_id']
|
|
|
|
#判断云端文件是否存在
|
|
|
|
#判断云端文件是否存在
|
|
|
|
|
|
|
|
try:
|
|
|
|
exist = is_exist(f'{shixun_id}.json')
|
|
|
|
exist = is_exist(f'{shixun_id}.json')
|
|
|
|
if exist: #存在,则跳转到云端下载并终止本程序
|
|
|
|
if exist: #存在,则跳转到云端下载并终止本程序
|
|
|
|
print('云端文件已存在,正在下载')
|
|
|
|
print('云端文件已存在,正在下载')
|
|
|
|
download(f'{shixun_id}.json')
|
|
|
|
download(f'{shixun_id}.json')
|
|
|
|
safari.close()
|
|
|
|
# 检测本地文件是否下载完成
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
if os.path.exists(f'{shixun_id}.json'):
|
|
|
|
|
|
|
|
print('下载完成')
|
|
|
|
|
|
|
|
safari.quit()
|
|
|
|
exit()
|
|
|
|
exit()
|
|
|
|
else: #不存在,则继续执行本程序
|
|
|
|
break
|
|
|
|
print('云端文件不存在,开始获取参数')
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
finally: #不存在,则继续执行本程序
|
|
|
|
#获取关卡数
|
|
|
|
#获取关卡数
|
|
|
|
#点击展开关卡页面
|
|
|
|
#点击展开关卡页面
|
|
|
|
safari.find_element(By.XPATH,'//*[@id="task-left-panel"]/div[1]/a[1]').click()
|
|
|
|
safari.find_element(By.XPATH,'//*[@id="task-left-panel"]/div[1]/a[1]').click()
|
|
|
@ -92,15 +105,8 @@ if is_practice(url):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
obj1 = re.compile(r'<h3 id="任务描述">任务描述</h3><p>(?P<describe>.*?)</p>',re.S)
|
|
|
|
obj1 = re.compile(r'<h3 id="任务描述">任务描述</h3><p>(?P<describe>.*?)</p>',re.S)
|
|
|
|
obj2 = re.compile(r'<h3 id="编程要求">编程要求</h3><p>(?P<require>.*?)</p>',re.S)
|
|
|
|
obj2 = re.compile(r'<h3 id="编程要求">编程要求</h3><p>(?P<require>.*?)</p>',re.S)
|
|
|
|
#初始化一个总的json文件,名称为课程的id
|
|
|
|
#初始化一个字典,用于存放所有关卡的参数
|
|
|
|
total = {}
|
|
|
|
total = {}
|
|
|
|
#另外,目前好像只有实训作业有这些参数,其他的作业例如编程作业就没有,所以先判断一下是否为实训作业
|
|
|
|
|
|
|
|
# def is_practice():
|
|
|
|
|
|
|
|
# try:
|
|
|
|
|
|
|
|
# safari.find_element(By.XPATH,'//*[@id="task-left-panel"]/div[1]/a[1]')
|
|
|
|
|
|
|
|
# return True
|
|
|
|
|
|
|
|
# except BaseException:
|
|
|
|
|
|
|
|
# return False
|
|
|
|
|
|
|
|
i=1
|
|
|
|
i=1
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
while i <= task_num:
|
|
|
|
while i <= task_num:
|
|
|
@ -139,17 +145,27 @@ if is_practice(url):
|
|
|
|
'describe':describe[0] if len(describe) != 0 else '',
|
|
|
|
'describe':describe[0] if len(describe) != 0 else '',
|
|
|
|
'require':require[0] if len(require) != 0 else '',
|
|
|
|
'require':require[0] if len(require) != 0 else '',
|
|
|
|
'code':code,
|
|
|
|
'code':code,
|
|
|
|
'verified': False #这个参数是用来标记答案是否被用户认证为正确答案的,初始值为False
|
|
|
|
'verified': False, #这个参数是用来标记答案是否被用户认证为正确答案的,初始值为False
|
|
|
|
|
|
|
|
'last_modified': time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) #这个参数是用来标记答案最后一次被修改的时间,初始值为当前时间
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#把每一关的参数存入总的字典中
|
|
|
|
#把每一关的参数存入总的字典中
|
|
|
|
total[challenge_id] = task
|
|
|
|
total[challenge_id] = task
|
|
|
|
#去往下一关
|
|
|
|
#去往下一关
|
|
|
|
i += 1
|
|
|
|
i += 1
|
|
|
|
except BaseException:
|
|
|
|
except BaseException:
|
|
|
|
print('获取参数失败')
|
|
|
|
print(f'{challenge_id}参数获取参数失败')
|
|
|
|
|
|
|
|
#判断爬取到的代码是否存在空值,如果存在空值,则重新爬取
|
|
|
|
|
|
|
|
for value in total.values():
|
|
|
|
|
|
|
|
if value['code'] == '':
|
|
|
|
|
|
|
|
print('检测到代码参数为空值,重新爬取')
|
|
|
|
|
|
|
|
safari.close()
|
|
|
|
|
|
|
|
os.system('python get_params.py')
|
|
|
|
|
|
|
|
exit()
|
|
|
|
#把参数写入本地json文件中,文件名字与shixun_name相同键为course_id,值为一个列表,列表中每个元素为一个字典,字典中包含每一关的参数
|
|
|
|
#把参数写入本地json文件中,文件名字与shixun_name相同键为course_id,值为一个列表,列表中每个元素为一个字典,字典中包含每一关的参数
|
|
|
|
with open(f'{shixun_id}.json','w',encoding='utf-8') as f:
|
|
|
|
with open(f'{shixun_id}.json','w',encoding='utf-8') as f:
|
|
|
|
json.dump(total,f,ensure_ascii=False,indent=4)
|
|
|
|
json.dump(total,f,ensure_ascii=False,indent=4)
|
|
|
|
|
|
|
|
#关闭浏览器
|
|
|
|
|
|
|
|
safari.quit()
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
print('这不是一个实训作业')
|
|
|
|
print('这不是一个实训作业')
|
|
|
|
exit()
|
|
|
|
exit()
|
|
|
|