马上要开始上课了,慕课网的中国近代史总是要想办法来逃掉的,去了GitHub翻到了前人的杰作。自动化看视频、文本。在线答题看来还得自己来想办法,给提供的思路和自己关键字搜索好像没多大区别,看来自动化播放也得不定时看一下是否要答题,建议稍微改一下脚本文件,检测到答题后进行播放音乐提醒用户回来答题。

 

 

README.MD

 

 

## 环境

- python 3.6

- 在项目目录使用

```pip install -r requirments.txt```

- ChinaMooc.py 仅限于Chrome浏览器,且需要配置chromedriver.exe

CODE

  1. # -*- coding: utf-8 -*- 
  2. # __Author__: Sdite 
  3. # __Email__ : a122691411@gmail.com 
  4.  
  5. from selenium import webdriver 
  6. from selenium.webdriver.support.select import Select 
  7. from getpass import getpass 
  8. import time 
  9.  
  10.  
  11. class Mooc(object): 
  12.  
  13.     def __init__(self, course_id=0, see_spooc=True): 
  14.         super(Mooc, self).__init__() 
  15.         self.mooc_url = 'https://www.icourse163.org/'       # 慕课账号 
  16.         self.flash_open_url = 'chrome://settings/content/siteDetails?site=%s' % self.mooc_url # flash打开的链接 
  17.         self.see_spooc = see_spooc                          # 布尔值表示看spooc的课程还是mooc课程 
  18.         self.course_id = course_id                          # 看慕课中的第几门课程 
  19.  
  20.  
  21.     def run(self): 
  22.         try
  23.             self.get_login_info()  # 获取登录账号 
  24.             self.driver = webdriver.Chrome()  # 打开浏览器 
  25.             self.driver.maximize_window() 
  26.             self.allow_flash()  # 允许使用flash 
  27.             self.driver.get(url=self.mooc_url)      # 打开慕课 
  28.  
  29.             self.login()                            # 登录 
  30.             self.enter_course()                     # 进入课程 
  31.             self.start_learning()                   # 开始学习 
  32.         except
  33.             print('[失败]: 网络不稳定,请重试或更换网络'
  34.  
  35.     def allow_flash(self): 
  36.         def expand_shadow_element(element): 
  37.             shadow_root = self.driver.execute_script('return arguments[0].shadowRoot', element) 
  38.             return shadow_root 
  39.  
  40.         # 打开chrome设置页面 
  41.         self.driver.get(self.flash_open_url) 
  42.  
  43.         # 允许flash 
  44.         root1 = expand_shadow_element(self.driver.find_element_by_css_selector('body > settings-ui')) 
  45.         root2 = expand_shadow_element(root1.find_element_by_css_selector('#main')) 
  46.         root3 = expand_shadow_element(root2.find_element_by_css_selector('settings-basic-page')) 
  47.         root4 = expand_shadow_element(root3.find_element_by_css_selector('#advancedPage > settings-section.expanded > settings-privacy-page')) 
  48.         root5 = expand_shadow_element(root4.find_element_by_css_selector('#pages > settings-subpage > site-details')) 
  49.         root6 = expand_shadow_element(root5.find_element_by_css_selector('#plugins')) 
  50.         root7 = root6.find_element_by_css_selector('#permission'
  51.         Select(root7).select_by_index(1
  52.  
  53.     def get_login_info(self): 
  54.         self.user_name = str(input('请输入慕课账号: ')) 
  55.         self.password = str(getpass('请输入密码: ')) 
  56.  
  57.  
  58.     def login(self): 
  59.         self.driver.find_element_by_xpath( 
  60.             '//*[@id="j-slideTop-slideBox-wrapper"]/div/div/div[3]/div/div/div[3]').click() 
  61.         time.sleep(2
  62.  
  63.         self.driver.switch_to.frame(self.driver.find_elements_by_tag_name('iframe')[0])           # 切换到登录的frame 
  64.  
  65.         # 切换账号密码 
  66.         click_object = self.driver.find_element_by_css_selector('.u-tab.f-cb').find_element_by_css_selector('.tab0'
  67.         self.driver.execute_script('arguments[0].click()', click_object) 
  68.         time.sleep(0.1
  69.  
  70.         self.driver.find_element_by_xpath('//*[@id="phoneipt"]').send_keys(self.user_name)        # 输入账号 
  71.         time.sleep(0.3
  72.         self.driver.find_element_by_class_name('j-inputtext').send_keys(self.password)            # 输入密码 
  73.         self.driver.find_element_by_xpath('//*[@id="submitBtn"]').click()                         # 登录 
  74.  
  75.         self.driver.switch_to.default_content()                                                   # 切换回原来的frame 
  76.         time.sleep(1.5
  77.  
  78.     def enter_course(self): 
  79.         # 进入我的课程 
  80.         self.driver.find_element_by_xpath( 
  81.             '//*[@id="j-slideTop-slideBox-wrapper"]/div/div/div[3]/div/div/div[3]/a/span').click() 
  82.  
  83.         time.sleep(1
  84.  
  85.         # 是否看的是spooc课程 
  86.         if self.see_spooc: 
  87.             self.driver.find_element_by_xpath('//*[@id="j-module-tab"]/div/div[2]').click() 
  88.  
  89.         time.sleep(1.3
  90.         # 打开课程 
  91.         self.driver.find_elements_by_xpath( 
  92.             '//*[@id="j-coursewrap"]/div/div[1]/div')[self.course_id].click() 
  93.         self.driver.close() 
  94.         time.sleep(1.5
  95.  
  96.     def open_all_chapters(self): 
  97.         # 展开所有章节 
  98.         chapters = self.driver.find_elements_by_class_name('j-up'
  99.         for chapter in chapters: 
  100.             try
  101.                 chapter.click() 
  102.             except
  103.                 pass 
  104.  
  105.     def enter_not_learn(self, type): 
  106.         # 进入课件 
  107.         self.driver.find_element_by_xpath('//*[@id="j-courseTabList"]/li[5]').click() 
  108.         time.sleep(1.5
  109.         self.open_all_chapters() 
  110.         time.sleep(2.5
  111.  
  112.         element = self.driver.find_elements_by_css_selector('.f-icon.lsicon.f-fl'
  113.  
  114.         # 找到没看过的视频开始看视频 
  115.         for e in element: 
  116.             title = e.get_attribute('title'
  117.             class_ = e.get_attribute('class'
  118.  
  119.             if type in title and 'learned' not in class_
  120.                 e.click() 
  121.                 return False 
  122.         return True 
  123.  
  124.     def see_video(self): 
  125.         video_finish = self.enter_not_learn(type='视频'
  126.  
  127.         if not video_finish: 
  128.             while True
  129.                 try
  130.                     double_speed = self.driver.find_element_by_css_selector('.m-popover.m-popover-rate') \ 
  131.                         .find_element_by_xpath('./ul/li[6]'
  132.                     self.driver.execute_script('arguments[0].click()', double_speed) 
  133.                     break 
  134.                 except
  135.                     pass 
  136.  
  137.             while True
  138.                 try
  139.                     try
  140.                         # 当看完视频时,才不会抛出异常,可以被直接break,否则不会break 
  141.                         self.driver.find_element_by_css_selector('.success.icon-成功'
  142.                         break 
  143.                     except
  144.                         # 弹出选择问题解决 
  145.                         self.driver.find_element_by_css_selector('.u-btn.u-btn-default.submit.j-submit').click() 
  146.                         time.sleep(0.1
  147.                         self.driver.find_element_by_css_selector('.u-btn.u-btn-default.cont.j-continue').click() 
  148.                 except
  149.                     time.sleep(3
  150.         print('[成功]: 视频已全看完(不一定包括直播)'
  151.  
  152.     def read_rich_text(self): 
  153.         while not self.enter_not_learn(type='富文本'): 
  154.             time.sleep(0.5
  155.         print('[成功]: 富文本已全看完'
  156.  
  157.     def read_document(self): 
  158.         while not self.enter_not_learn(type='文档'): 
  159.             while True
  160.                 try
  161.                     time.sleep(10)  # 给flash足够的加载时间 
  162.                     box = self.driver.find_element_by_css_selector('.j-unitctBox.unitctBox'
  163.                     self.driver.execute_script('arguments[0].style.height="100000px"', box) 
  164.                     break 
  165.                 except
  166.                     pass 
  167.  
  168.         print('[成功]: 文档已全看完'
  169.  
  170.     def start_learning(self): 
  171.         self.driver.switch_to.window(self.driver.window_handles[0]) 
  172.         time.sleep(1
  173.  
  174.         self.see_video()                        # 看视频 
  175.         self.read_rich_text()                   # 看富文本 
  176.         self.read_document()                    # 看文档 
  177.  
  178.  
  179. if __name__ == '__main__'
  180.     # 表示course_id看第一门课程 
  181.     mooc = Mooc(course_id=0, see_spooc=True
  182.     mooc.run()