目录
- 好久不见
- 实现过程
- 一步拿捏讨论
- 美图
好久不见
写这篇文章只是想证明一下:本博主还在呼吸
许久未更,甚是想更呐~
这段时间生活中充斥着各种事情,感觉每天都在忙忙碌碌,偶而停下疲惫的身躯,突然抬头,却不知自己身处何方,忙了这么久,到底在忙些什么呢?找不着方向,于是开始惶恐,便又不知方向的胡乱奔走……,好吧好吧,总结两个字:迷茫,再加两个字:瞎忙……
其实这几天一直想写博客的,想写有趣又轻松的一篇文章,但又不到写哪些内容比较好,又因还要解决各种事情,于是就搁到了现在……
直到某天,为了完成MOOC(MOOC不会没人不知道吧)上的回复讨论任务,我不已为然的点开了MOOC……突然?我看到了一个进度条,那个进度条仿佛就像是深渊,脑海中突然闪出一句话,我%@#!,咳咳咳,不是不是,是一句名言:当你凝望深渊,深渊也在凝望着你……
(进度条是已经用代……不是,是已经用我辛勤的劳动把它拉起来的)
不知到有没有小伙伴经历过要回复几十条讨论的经历,当场看见那进度条我就……,就偷偷写脚本了
这个脚本花了我比较长的时间才完成的,因为调试的过程有些许麻烦……,不过最终还是解决了,想看效果如何,请往下看
实现过程
老操作了兄弟们~
又双叒叕是Get_cookie.py了兄弟,看过我《小玩意儿》专栏文章的都能看烂了,但别说,还真好用,登录一次,一劳永逸,就靠它了~,话不多说,直接上号
上码
(记得在三十秒内登录哦~)
from selenium import webdriver
import time
import json
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.icourse.org/')
time.sleep()
dictCookies = driver.get_cookies()
jsonCookies = json.dumps(dictCookies) # 转换成字符串保存
with open('moocCookie.txt', 'w') as f:
f.write(jsonCookies)
print("cookies保存成功")
一步拿捏讨论
以前回复讨论咱们讲究的是什么?讲究的是灵活运用Ctrl C和Ctrl V,讲究的是快准狠
现在回复讨论咱们讲究的是什么?讲究的是能不用手就不用手,讲究的是一个 automation,科技是第一生产力呀兄弟们
什么?相应模块怎么下载?pip install 模块名 就完事了兄弟~
什么?chromedriver怎么配置?看这篇文章就完事兄弟~
【Python网络蜘蛛 · 9】:selenium模块【安装和配置环境变量教程、提取数据操作、无头模式】(附操作源码)
什么?不会搞?看下图干就完事了兄弟
实现代码
话不多说码来
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import json
import time
class MOOC(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get('https://www.icourse.org/')
def login(self):
#获取保存下的cookie值
with open('moocCookie.txt', 'r', encoding='utf-') as f:
listCookies = json.loads(f.read())
# 往driver里添加cookie
for cookie in listCookies:
cookie_dict = {
'domain': '.icourse.org',
'name': cookie.get('name'),
'value': cookie.get('value'),
'expires': '',
'path': '/',
'httpOnly': False,
'HostOnly': False,
'Secure': False
}
self.driver.add_cookie(cookie_dict)
self.driver.refresh() # 刷新页面
# 进入课程
def entering_the_course(self):
#等待“个人中心”元素出现
WebDriverWait(self.driver,).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="j-indexNav-bar"]/div/div/div/div/div[]/div[3]/div/div/a/span'))
)
self.driver.find_element(By.XPATH, '//*[@id="j-indexNav-bar"]/div/div/div/div/div[]/div[3]/div/div/a/span').click()
# 等待“SPOC课程”元素出现并点击
time.sleep()
WebDriverWait(self.driver,).until(
EC.presence_of_element_located((By.XPATH, '//div[@class="u-selectTab-container"]/div[]/a'))
)
self.driver.find_element(By.XPATH, '//div[@class="u-selectTab-container"]/div[]/a').click()
#等待课程元素出现,并获取元素,用户选择课程
WebDriverWait(self.driver,).until(
EC.presence_of_element_located((By.XPATH, '//div[@class="course-card-wrapper"]/div/a/div/img'))
)
course_ele = self.driver.find_elements(By.XPATH, '//div[@class="course-card-wrapper"]/div/a/div/img')
course_name = [ele.get_attribute('alt') for ele in course_ele]
for index, i in enumerate(course_name):
print(index, i)
user = int(input('请输入课程序号:'))
course_ele[user].click()
time.sleep()
self.driver.switch_to.window(self.driver.window_handles[-]) # 切换窗口
time.sleep()
#等待课件出现并点击
WebDriverWait(self.driver,).until(
EC.presence_of_element_located((By.XPATH, '//li[@data-name="课件"]'))
)
self.driver.find_element(By.XPATH, '//li[@data-name="课件"]').click()
# 等待章节元素出现并点击
WebDriverWait(self.driver,).until(
EC.presence_of_element_located((By.XPATH, '//div[@class="m-learnChapterList f-pr"]/div[@class="m-learnChapterNormal f-pr"][]'))
)
# 点击章节
self.driver.find_element(By.XPATH, '//div[@class="m-learnChapterList f-pr"]/div[@class="m-learnChapterNormal f-pr"][]').click()
time.sleep(.3)
# 点击进入视频
self.driver.find_elements(By.XPATH, '//div[@class="sourceList"]/div[]')[0].click()
# 获取章节中”选择课程“的元素并点击
WebDriverWait(self.driver,).until(
EC.presence_of_element_located(
(By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div'))
)
# 获取所有章节
temp_chapters = self.driver.find_elements(By.XPATH, '//div[@class="f-fl j-chapter"]//div[@class="down f-bg j-list"]/div')
# 通过循环获取到的章节元素和课程元素,进行讨论
for chapter_index in range(len(temp_chapters)): # 循环章节
# 获取章节中”选择课程“的元素并点击
WebDriverWait(self.driver,).until(
EC.presence_of_element_located(
(By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div'))
)
# 获取章节选项框并点击
self.driver.find_element(By.XPATH, '//div[@class="f-fl j-chapter"]/div[@class="u-select"]/div[]').click()
self.driver.find_elements(By.XPATH, '//div[@class="f-fl j-chapter"]//div[@class="down f-bg j-list"]/div')[chapter_index].click()
# 等待课程元素出现
WebDriverWait(self.driver,).until(
EC.presence_of_element_located(
(By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div'))
)
# 获取章节中所有课程
course = self.driver.find_elements(By.XPATH,'//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div/div[]/div')
course_names = [i.get_attribute('title') for i in course]
for c_index in range(len(course)): # 循环课程
WebDriverWait(self.driver,).until(
EC.presence_of_element_located(
(By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div'))
)
self.driver.find_element(By.XPATH,'//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div').click()
WebDriverWait(self.driver,).until(
EC.presence_of_element_located(
(By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div/div[]/div'))
)
# 重新获取课程元素并点击
course = self.driver.find_elements(By.XPATH, '//div[@class="j-breadcb f-fl"]//div[@class="f-fl j-lesson"]/div/div[2]/div')
course[c_index].click()
time.sleep()
try:
try:
# 获取讨论元素
disc = self.driver.find_element(By.XPATH, '//li[@title="讨论:讨论话题"]/span[]')
if disc.is_enabled(): # 如果讨论元素存在且可以点击
disc.click()
self.discuss() # 调用讨论函数
except:
time.sleep(.5)
if course_names[c_index] == '讨论区':
self.discuss()
except:
pass
# 讨论
def discuss(self):
# 等待“回复”元素出现
WebDriverWait(self.driver,).until(
EC.presence_of_element_located((By.XPATH, '//div[@class="optbar f-cb"]/a[]/span'))
)
time.sleep(.5)
self.driver.execute_script("var q=document.documentElement.scrollTop=")
time.sleep(.5)
try:
content = self.driver.find_element(By.XPATH, '//div[@class="j-list"]/div/div[]/div/div[2]/p').text
except:
content = self.driver.find_element(By.XPATH, '//div[@class="j-list"]/div/div[]/div/div[2]').text
self.driver.switch_to.frame()
time.sleep(.5)
# 定位输入框并输入信息
self.driver.find_element(By.XPATH, '//body[@contenteditable="true"]').send_keys(content)
self.driver.switch_to.window(self.driver.window_handles[-]) # 进行窗口切换
time.sleep()
# 点击发送回复
self.driver.find_element(By.XPATH,'/html/body/div[]/div[2]/div[4]/div[2]/div/div[1]/div/div/div[3]/div[2]/div[2]/div/div/div[5]/div/div[1]/div[2]/a[1]').click()
def run(self):
self.login()
self.entering_the_course()
mooc = MOOC()
mooc.run()