python雨课堂答案_python实现雨课堂自动预习
用selenium和python的win32api库实现了一个雨YU课堂自动预习的脚本,第一次需要登录,会自动存储cookies,
全程只需要手动输入网址,和页数其他自动完成,
如果里面有题目的话,这个脚本还做不了哈,而且使用的时候鼠标要放在浏览器窗口
,selenium没有实现鼠标滚轮滑动的库。
有也是旁边要有scroll,而YU课堂没有,
就想到了win32api,但这也是缺陷,没有涉及底层的代码,
会js的小伙伴可以试一试用js完成中间的浏览步骤,可能代码更简洁。
下面放上代码,
仅供学习交流,不是偷懒的工具哦!!!!!!!!!!
# coding:utf-8# time:2020.6.5.19:30# auther:小生# website:fromseleniumimportwebdriver
importtime
importwin32api
importwin32con
fromdatetimeimportdatetime
fromdatetimeimporttimedelta
importrandom
importos
importjson
"""使用时,请将鼠标放到浏览器内,暂时没有想到更好的方法,要时刻观察控制台,自动打开浏览器后会提示你输入网址,如果第一次使用,没有可供使用的cookies,那么控制台会提示登录,30秒足够了。没有设置selenium的页面等待,然后会自动打开页面,然后控制台会提示输入页数,输入即可,鼠标放到浏览器窗口,你可以吃饭,去上厕所,或者去玩,总之你开心就好。"""classRainclass(object):
driver_path =r"D:\Python\chromedriver\chromedriver.exe"time_all =0num =1def__init__(self):
self.driver = webdriver.Chrome(executable_path=Rainclass.driver_path)
self.url =input("请输入要自动完成页面的网址:")
# self.url = "https://changjiang.yuketang.cn/v2/web/studentCards"self.driver.get(self.url)
definitial_judge(self):
ifos.path.exists("rainclass.txt"):#判断是否当前文件夹有存在存储cookies的文件print("cookies存在")
self.send_cookies()
else:
self.get_cookies()
print("cookies不存在")
defget_cookies(self):
print("将沉睡30秒,瞅啥呢,赶紧登录,老子要拿cookies")
time.sleep(30)
distcookies =self.driver.get_cookies()
jsoncookies = json.dumps(distcookies)
withopen("rainclass.txt","w")asfp:
fp.write(jsoncookies)
print("拿取cookies成功,正在进行下一步,登录")
time.sleep(1.5)
self.send_cookies()
defsend_cookies(self):
withopen("rainclass.txt","r",encoding="utf-8")asfp:
liscookies = json.loads(fp.read())
forcookieinliscookies:
cookie_dic = {
"domain":"changjiang.yuketang.cn","expiry": cookie.get("expiry"),"httpOnly": cookie.get("httpOnly"),"name": cookie.get("name"),"path":"/","secure": cookie.get("secure"),"value": cookie.get("value")
}
self.driver.add_cookie(cookie_dic)#把cookies加入并刷新self.driver.refresh()
time.sleep(2)#别给人服务器造成太大压力self.run()
defrun(self):
first_page =self.driver.find_element_by_xpath('//div[@class="ppt_img_box"]')
first_page.click()
print("此页面将停留2秒,页面将自动打开,请将鼠标放在浏览器窗口")
# self.driver.set_window_size(1000,300000)# self.driver.execute_script("window.scrollBy(0,1000)","")all_page =int(input("请输入页数:"))
selector =self.driver.find_element_by_xpath('//div[@class="slide_layer"]')
selector.click()
time.sleep(3)
# all_seconds = all_page*5+0.5self.time0 = datetime.now()
while True:
# if datetime.now()-self.time0
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL,0,0,-1)
time_li = [4,4.2,4.3,4.4,4.5,4.6,4.7,4.8,4.9,5,5.21,5.3,5.4,5.5,5.6,5.7,7.8,5.9,6
time_rand = random.choice(time_li)
time.sleep(time_rand)
print(str("预习秒数为:")+str(time_rand))
# self.time_all = self.time_all+time_randself.num+=1print("第{}页可能已经预习完成".format(self.num))
else:
print("*"*20+"预习可能已经完成"+"*"*20)
print("*"*20+"预习可能已经完成"+"*"*20)
breakif__name__ =='__main__':
clas = Rainclass()
clas.initial_judge()