介绍
基于python写的,使用前请先配置好webdriver,详情请戳:webdriver配置
BUG
使用时可能会跳过一些章节,刷课的效果应该还行因为有大佬写的脚本所以没实用过,答题容错能力差答案写死了的,目前答案有限,如果你有答案可以联系我添加上去,当然还要更多的等你发现哦。 

话不多说直接上代码
代码不在里面
骗你的 

# -*- coding: utf-8 -*-
'''
@Time : 2019.12.18 13:55
@Author : Aisen
@FileName: main.py
@Software: PyCharm
'''
from selenium import webdriver
from requests import get
import re
from time import sleep, strftime, localtime
class Mooc(object):
def __init__(self):
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
self.driver = webdriver.Chrome(options=options)
self.driver.implicitly_wait(2)
self.in_time = strftime("%Y-%m-%d %H:%M:%S", localtime())
def login(self,id,pwd):
url = 'http://www.neumooc.com/login'
self.driver.get(url)
self.driver.find_element_by_name('userName').send_keys(id)
self.driver.find_element_by_name('password').send_keys(pwd)
print('请输入验证码,并手动点击登录!')
while True:
try:
if self.driver.current_url == "http://www.neumooc.com/index?auto=1":
print("登录成功!")
print("请选择课程,并在浏览器打开!")
break
except:
pass
def get_courses(self):
try:
url = 'http://nsu.zhonghaitao.com/courses.txt'
courses = eval(get(url).text)
return courses
except:
return {}
def get_answer(self,url):
try:
# url = 'http://mooc.neumooc.com/course/play/init?courseId=C67744D352EB42B199D5905E7167AB97&outlineId=F08F5B8F4FCE4E76A4DE77BB280F197B'
courseId = re.search(r"courseId=[a-zA-Z0-9]*", url,).group().split("=")[-1]
# print(courseId)
courseIds = dict(self.get_courses())
# print(courseIds)
if courseId in courseIds:
answer_url = 'http://nsu.zhonghaitao.com/answers/' + courseIds[courseId]
# print(answer_url)
r = get(answer_url)
r.encoding = 'utf-8'
answer = r.text
answer = answer.split('\n')
# print(answer)
end_answer = {}
for i in range(len(answer)):
answer[i] = answer[i].strip()
key = answer[i].split(':')[0]
value = answer[i].split(':')[-1].split(' ')
end_answer[key] = value
# print(answer)
# print(end_answer)
return end_answer
else:
print("没有该课程答案,请联系管理员添加。")
return {}
except:
print('获取答案失败!请联系管理员修复!')
return {}
def click_course(self, url, answer, num=0):
'''
courses 'class=childLi outl_F5B998F7AA3C49DA83E5AA6F00ECE73E'
测试 'class=seq_html inactive text-center'
childLi outl_5C5A29558B804FEEA0956195A6491C86
self.driver.find_elements_by_xpath("//a[@class='childLi']/a")
:param url:
:return:
'''
try:
self.driver.get(url)
while self.is_500():
self.driver.refresh()
finsh_list = []
test_elements = self.driver.find_elements_by_class_name('childLi')
for i in range(len(test_elements)):
info = test_elements[i].text
id_info = info.split(' ')[0]
#print(id_info)
if '未完成' not in info and '完成' in info:
finsh_list.append(id_info)
print(finsh_list)
test_elements = self.driver.find_elements_by_class_name('outlineDiv')
for i in range(num, len(test_elements)):
sleep(1)
while self.is_500():
self.driver.refresh()
js = "var q=document.getElementById('chapterList').scrollTop=" + str(num // 4 * 100)
self.driver.execute_script(js)
try:
test_elements[i].click()
except:
num += 1
continue
test_elements = self.driver.find_elements_by_class_name('outlineDiv')
info = self.driver.find_element_by_class_name('mooc-commen-color').text
id_info = info.split(' ')[0]
if self.is_video() and id_info not in finsh_list:
print('{}:开始学习。'.format(id_info))
self.start_video()
print('{}:学习完成。'.format(id_info))
elif self.is_video() and id_info in finsh_list:
print('{}:视频已经学习过。'.format(id_info))
if id_info in answer:
# 点击测试
self.driver.find_element_by_class_name('seq_html').click()
sleep(2)
score = int(self.driver.find_element_by_id('maxCorrectRate').get_attribute('innerText')[:-1])
if score 0:
return True
else:
return False
def start_video(self):
'''
video class="fp-ui"
播放 class="fp-icon fp-playbtn"
现在时间 class="fp-elapsed"
视频时间 class="fp-duration"
:return:
'''
try:
sleep(1)
video_time = self.driver.find_element_by_class_name('fp-duration').get_attribute('innerText')
while video_time == '':
video_btn = self.driver.find_element_by_class_name('fp-ui').click()
video_time = self.driver.find_element_by_class_name('fp-duration').get_attribute('innerText')
stop_btns = self.driver.find_elements_by_class_name('fp-playbtn')
check_time = ''
while True:
try:
sleep(1)
if self.is_500():
break
video_time = self.driver.find_element_by_class_name('fp-duration').get_attribute('innerText')
now_time = self.driver.find_element_by_class_name('fp-elapsed').get_attribute('innerText')
if now_time == video_time:
#print('播放完毕!')
break
else:
if check_time >= now_time:
stop_btn = stop_btns[-1]
stop_btn.click()
check_time = now_time
except:
pass
except:
self.driver.refresh()
self.start_video()
def start_test(self, answer):
''''
获取课程测试id //*[@id="testQuesId"]
A:class = radio pull-left text-left
//*[@id="9A45DABE973D495C97690872CE91DB1C"]/section[3]/label
//*[@id="9A45DABE973D495C97690872CE91DB1C"]/section[4]
'''
try:
for i in range(len(answer)):
sleep(1)
id = self.driver.find_element_by_xpath('//*[@id="testQuesId"]')
idValue = id.get_attribute('value') # 获取value的内容
for q in answer[i]:
if self.is_500():
break
if q == 'A':
self.driver.find_element_by_xpath('//*[@id="' + idValue + '"]/section[3]/label').click()
elif q == 'B':
self.driver.find_element_by_xpath('//*[@id="' + idValue + '"]/section[4]/label').click()
elif q == 'C':
self.driver.find_element_by_xpath('//*[@id="' + idValue + '"]/section[5]/label').click()
elif q == 'D':
self.driver.find_element_by_xpath('//*[@id="' + idValue + '"]/section[6]/label').click()
elif q == 'E':
self.driver.find_element_by_xpath('//*[@id="' + idValue + '"]/section[7]/label').click()
elif q == 'F':
self.driver.find_element_by_xpath('//*[@id="' + idValue + '"]/section[8]/label').click()
sleep(0.1)
self.driver.find_element_by_xpath('//*[@id="testContent"]/div/div[3]/button').click()
if self.is_500():
break
sleep(0.1)
self.driver.find_element_by_class_name('ui-dialog_art-autofocus').click()
sleep(0.1)
self.driver.find_element_by_class_name('ui-dialog_art-button').click()
except:
self.driver.refresh()
self.driver.find_element_by_class_name('seq_html').click()
sleep(1)
score = int(self.driver.find_element_by_id('maxCorrectRate').get_attribute('innerText')[:-1])
if score
可以奥,我之前用pyqt5+无头浏览器打包成exe
来康康大佬(
视频不能自动刷了...