-
Notifications
You must be signed in to change notification settings - Fork 5
/
selenium刷视频.py
216 lines (190 loc) · 8.37 KB
/
selenium刷视频.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from datetime import datetime
import requests
from selenium import webdriver
import time
import sys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 全局超时时间
TIME_OUT = 100
# 防止503等待时间
WAIT_TIME = 2
# 具体可添加QQ 1141549077 交流
print('参数列表:', str(sys.argv))
# 根据参数动态获得配置文件,以及浏览器的用户配置
# 第一个参数是文件名 , 第二个参数是参数
courseCode = ""
if len(sys.argv) > 1:
courseCode = sys.argv[1]
print("参数为:"+ courseCode)
startTime = datetime.now()
print("************************开始刷视频啦 "+ str(startTime)+"*******************************")
cookies = []
userName = ""
password = ""
with open("user.txt","r",encoding="UTF-8") as configFile:
for line in configFile:
# 去除换行符
lineSplit = line.strip().split(":")
userName = lineSplit[0]
password = lineSplit[1]
print("用户名:"+userName+" 密码:"+password)
print("step1 登录")
option = webdriver.ChromeOptions()
# 隐藏窗口
option.add_argument('headless')
option.add_experimental_option("excludeSwitches", ['enable-automation','enable-logging'])
driver = webdriver.Chrome(chrome_options=option)
# 模拟登陆
driver.get("http:https://student.ouchn.cn/")
WebDriverWait(driver, TIME_OUT).until(EC.presence_of_element_located((By.ID, "username")))
userNameInput = driver.find_element_by_id("username")
pwdInput = driver.find_element_by_id("password")
userNameInput.send_keys(userName)
pwdInput.send_keys(password)
driver.find_element_by_name("button").click()
WebDriverWait(driver, TIME_OUT).until(EC.presence_of_element_located((By.XPATH, "//button[text()='进入学习']")))
allCourse = driver.find_elements_by_xpath("//button[text()='进入学习']")
#http:https://ruanjian.ouchn.cn/course/view.php?id=5055
handleOrigin = driver.current_window_handle
url = None
for course in allCourse:
# print(">>开始刷视频,课程为:"+dict[courseCode])
# 切换到课程学习列表页面
driver.switch_to.window(handleOrigin)
course.click()
handles = driver.window_handles # 获取当前浏览器的所有标签页
driver.switch_to.window(handles[1])
WebDriverWait(driver, TIME_OUT).until(EC.presence_of_element_located((By.TAG_NAME, "title")))
hostName = str(driver.current_url).split("/")[2]
print("SpileUrl : " + str(str(driver.current_url).split("/")))
print("HostName:"+hostName)
# 判断是否是 需要刷的课程
if courseCode not in driver.current_url:
print("该课程不是需要刷的课程:" + driver.title)
driver.close()
continue
print("Cookie:" + str(driver.get_cookies()))
cookieHeader = ""
for cookie in driver.get_cookies():
cookieHeader += cookie['name'] + "=" + cookie['value'] + ";"
cookieHeader = cookieHeader[:-1]
requestHeader = {
"Host": hostName,
"Connection": "keep-alive",
"Accept": "*/*",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/87.0.4280.88Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded",
"Accept-Encoding": "gzip,deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cookie": cookieHeader
}
# 每个专题中取一个视频即可
# //ul[@class="flexsections flexsections-level-2"]/li
pageIdList = []
ids = []
sectionDivOverviews = driver.find_elements_by_css_selector(".section.main.aft")
for sectionItem in sectionDivOverviews:
# 每个里面取一个
videoItems = sectionItem.find_elements_by_tag_name("a");
if len(videoItems) < 1 :
continue
# 视频支取一个,而文章页面全部取出来
isVideo = True
for videoItem in videoItems:
href = videoItem.get_attribute("href")
hrefSplit = str(href).split('=')
imgTag = videoItem.find_elements_by_tag_name("img")
if len(imgTag) < 1 :
continue
# core_h 是视频 page_h是页面 , core_h打开需要提交表单,page_h不需要提交表单,关闭即可
imgSrc = imgTag[0].get_attribute("src")
#print("imgSrc:"+imgSrc)
if "core_h.png" in imgSrc and isVideo:
id = {}
id['val'] = hrefSplit[1]
id['attr'] = 'url'
ids.append(id)
isVideo = False
if "page_h.png" in imgSrc:
pageIdList.append(hrefSplit[1])
print("所有视频的ID:"+str(ids))
print("所有文章的ID:" + str(pageIdList))
# 循环每个专题中的首个未看过的 视频Id
sectionName = ""
for id in ids:
# 防止503异常
time.sleep(WAIT_TIME)
print(">>>> step3 开始打开视频网页")
# 打开视频页面
videoUrl = "http:https://"+hostName+"/mod/"+id["attr"]+"/view.php?id=" + id['val']
driver.get(videoUrl)
# 有可能不是视频网页
WebDriverWait(driver, TIME_OUT).until(EC.presence_of_element_located((By.TAG_NAME, "title")))
palyBtn = driver.find_elements_by_xpath('//*[@data-title="点击播放"]')
# 不是视频页面,直接回退 , 不在这里判断视频,在下边重新获取后
# if len(palyBtn) == 0:
# print(">>>> 3.1 不是视频网页,回退:" +videoUrl )
# driver.back()
# continue
# 获取专题的名称
sectionDiv = driver.find_elements_by_id("list");
if len(sectionDiv) ==1:
span = sectionDiv[0].find_elements_by_tag_name("span");
if len(span) == 1:
sectionName = span[0].text
# 重新获取专题所有的视频 id
videoIdList = []
videoIdListInPage =driver.find_elements_by_xpath('//li[@typ="url"]')
# 获取需要点开视频网站的ID
pageLiListInPage = driver.find_elements_by_xpath('//li[@typ="page"]')
for videoLi in videoIdListInPage:
# 该视频已经看过
if "url(c).png" in videoLi.find_element_by_tag_name("img").get_attribute("src"):
continue
# 打开页面
videoIdList.append(videoLi.get_attribute("i"))
# 打开视频网页并迅速完成
for videoId in videoIdList:
# 防止503异常
time.sleep(WAIT_TIME)
videoUrl = "http:https://"+hostName+"/mod/url/view.php?id=" + videoId
driver.get(videoUrl)
WebDriverWait(driver, TIME_OUT).until(EC.presence_of_element_located((By.TAG_NAME, "title")))
# 获取播放按钮
palyBtn = driver.find_elements_by_xpath('//*[@data-title="点击播放"]')
# 不是视频页面,直接回退
if len(palyBtn) == 0:
print(">>>> 3.2 不是视频网页,回退:" + driver.title)
driver.back()
continue
urlSpilt = str(driver.current_url).split("?")
# 拼接新参数 mid -> cmid
requestParam = urlSpilt[1].replace("mid","cmid")
# 直接请求完成视频
response = requests.get("http:https://"+hostName+"/theme/blueonionre/modulesCompletion.php?"+requestParam , headers=requestHeader)
print("返回结果: status_code:"+str(response.status_code) +" content:" +str(response.content) )
print(">>>> 该专题视频已全部刷完,专题:" + sectionName)
# 打开page文章页面,因为文章页面已获取全部
print(">>>>Step4 开始打开文章页面")
for pageId in pageIdList:
# 防止503异常
time.sleep(WAIT_TIME)
# http:https://shandong.ouchn.cn/mod/page/view.php?id=889096
pageUrl = "http:https://" + hostName + "/mod/page/view.php?id=" + pageId
# print("打开文章:"+pageUrl)
print("打开文章:" + driver.title)
driver.get(pageUrl)
print(">>>>Step4 文章页面已全部打开")
#关闭当前标签页
print("******* 该课程已刷完 : " + driver.title + "***********")
driver.close()
# 课程刷完,因为是单课程直接退出循环
break
endTime = datetime.now()
speedTime = (endTime-startTime).seconds
print("***************全部视频已刷完 "+ str(endTime)+"*************************" + "共耗时间(秒):"+ str(speedTime) )
# 关闭所有窗口
driver.quit()