过年回家抢票,让光猫自动重启的小脚本

Posted brianlai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了过年回家抢票,让光猫自动重启的小脚本相关的知识,希望对你有一定的参考价值。

 1 from selenium import webdriver
 2 from time import sleep
 3 import datetime
 4 url = "http://192.168.1.1/login_smart.html"
 5 url_login = "http://192.168.1.1/login.cgi"
 6 url_longout = "http://192.168.1.1/login.cgi#"
 7 Rstart_time = datetime.datetime.now().strftime(%Y-%m-%d %H:%M:%S)
 8 i = 0
 9 def write_record(now_log):
10     with open(r"C:UsersrianDesktopRestartLogslog.txt","r+") as f:
11         old = f.read()
12         f.seek(0,0)
13         f.writelines(now_log)
14         f.writelines(
)
15         f.writelines(old)
16         f.close()
17 
18 while True:
19     try:
20         dr = webdriver.Chrome()
21         dr.get(url)
22         dr.maximize_window()
23         sleep(0.1)
24         dr.find_element_by_css_selector("#loid_user").clear()
25         sleep(0.1)
26         dr.find_element_by_css_selector("#loid_user").send_keys("useradmin")
27         sleep(0.1)
28         dr.find_element_by_css_selector("#loid_pwd").send_keys("a124689a")
29         sleep(0.1)
30         dr.find_element_by_css_selector("#save_loid").click()
31         sleep(10)
32         if dr.current_url == url_login:
33             print("登录成功")
34         dr.find_element_by_css_selector("[alt=‘重启‘]").click()
35         sleep(0.1)
36         dr.find_element_by_css_selector("#okbutton1").click()
37         if dr.current_url == url_longout:
38             new_log = "重启成功,重启时间是:%s" %Rstart_time
39             print(new_log)
40             write_record(new_log)
41             dr.quit()
42             break
43         else:
44             dr.quit()
45             print("重启失败了,关闭浏览器再来")
46             continue
47     except:
48         dr.quit()
49         print("代码运行出错,关闭浏览器再来")
50         i = i +1
51         if i >= 3:
52             break
53         else:
54             continue

 

以上是关于过年回家抢票,让光猫自动重启的小脚本的主要内容,如果未能解决你的问题,请参考以下文章

GitHub 上的 12306 抢票神器,助力回家过年

春运到了,带你用python来抢票回家!

快过年了,Python实现12306查票以及自动购票....

python3.7之12306抢票脚本实现

利用Python自动抢火车票真的不难,100行代码即可完成

GO-HOME我要回家JAVA2013版火车票抢票程序