服务器之家

服务器之家 > 正文

python自动查询12306余票并发送邮箱提醒脚本

时间:2021-02-22 00:41     来源/作者:一枚空指针

由于车票难抢,有时需要的车票已经售空,而我们需要捡漏,便可使用这个脚本。

具体实现了,自动查询某一车票的余票数量,当数量产生变化时,将自动发送qq邮件到对于的邮箱进行提醒。

其中,发送邮箱的部分详情可参考我上一篇博文:python 发送邮件 (smtp) qq邮箱

脚本使用了python + selenium + chrome webdriver,需要先配置好三者的环境

具体使用时候,需自行改变代码中的

邮箱信息、出发站、到达站、车次、日期、以及到相应查询页面中找到对应车票的html id:

在相应查询页面中按f12,弹出开发者模式,点击python自动查询12306余票并发送邮箱提醒脚本按钮,再点击对应车票的框,python自动查询12306余票并发送邮箱提醒脚本python自动查询12306余票并发送邮箱提醒脚本,复制相应的id号到代码中对应处。

代码如下:

?
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
#coding:utf8
from selenium import webdriver
from selenium.webdriver.common.keys import keys
 
import time
from smtplib import smtp_ssl
from email.header import header
from email.mime.text import mimetext
 
from_station = 'sh'
to_station = 'nj'
train_num = 'g14'
train_date = '2018-02-14'
 
zyid = 'zy_5l00000g1441' #通过浏览器找到要查询车票的三个id
zeid = 'ze_5l00000g1441'
wzid = 'wz_5l00000g1441'
 
def send_email(train_date, train_num, one_num, two_num, wz_num): #函数中邮箱信息改为自己的
 email_from = "123456@qq.com"
 email_to = "654321@qq.com"
 hostname = "smtp.qq.com"
 login = "123456@qq.com"
 password = "xddflwjfkkfkbidf"
 subject = "train_tickets"
 text = ("日期:%s 车次:%s 一等座:%s 二等座:%s 无座:%s "%(train_date, train_num, one_num, two_num, wz_num))
   
 smtp = smtp_ssl(hostname)
 smtp.login(login, password)
 
 msg = mimetext(text, "plain", "utf-8")
 msg["subject"] = header(subject, "utf-8")
 msg["from"] = email_from
 msg["to"] = email_to
 
 smtp.sendmail(email_from, email_to, msg.as_string())
 smtp.quit()
 
driver = webdriver.chrome()
driver.get('https://kyfw.12306.cn/otn/index/init')
 
elem = driver.find_element_by_id("fromstationtext")
elem.clear()
elem.send_keys(from_station)
elem.send_keys(keys.enter) #写入始发站
 
elem = driver.find_element_by_id("tostationtext")
elem.clear()
elem.send_keys(to_station)
elem.send_keys(keys.enter) #写入终点站
 
js = "document.getelementbyid('train_date').removeattribute('readonly')" #去除日期栏只读属性
driver.execute_script(js)
 
elem=driver.find_element_by_id("train_date")
elem.clear()
elem.send_keys(train_date) #写入乘车日期
elem.click()
 
elem=driver.find_element_by_id("dc") #选择单程
elem.click()
 
elem=driver.find_element_by_id("a_search_ticket") #开始查询
try:
 elem.click() 
except exception:
 time.sleep(2)
 
query_times = 0
onetickets_last = 0
twotickets_last = 0
wztickets_last = 0
 
time.sleep(2
while(1):
 query_times = query_times+1
 text=""
 try:
  driver.find_element_by_id("query_ticket").click() #根据查询键是否可以找到,以判断页面显示正确与否
 except exception:
  driver.refresh()
  time.sleep(5);
  print("bug");
  continue
 
 print("第%d次查询:"%(query_times))
 time.sleep(5)
 
 try:
  text = driver.find_element_by_id(zyid).text
 except exception:
  driver.refresh()
  time.sleep(5);
  print("bug")
  continue
 if not(driver.find_element_by_id(zyid).text):
  driver.find_element_by_id("query_ticket").click()
  
 text = driver.find_element_by_id(zyid).text #查询一等座余票
 if(text == "有" or text == "无"):
  onetickets_now = text
  print("%s 一等座 : %s"%(train_num, onetickets_now))
  if(onetickets_now != onetickets_last):
   onetickets_last = onetickets_now
   #send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last)
 else:
  onetickets_now = text
  print("%s 一等座剩余票数 : %s"%(train_num, onetickets_now))
  if(onetickets_now != onetickets_last):
   onetickets_last = onetickets_now
   #send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last)
 
 text = driver.find_element_by_id(zeid).text #查询二等座余票
 if(text == "有" or text == "无"):
  twotickets_now = text
  print("%s 二等座 : %s"%(train_num, twotickets_now))
  if(twotickets_now != twotickets_last):
   twotickets_last = twotickets_now
   send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last)
 else:
  twotickets_now = text
  print("%s 二等座剩余票数 : %s"%(train_num,twotickets_now))
  if(twotickets_now != twoticketstwotickets_old):
   twotickets_last = twotickets_now
   send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last)
 
 text = driver.find_element_by_id(wzid).text #查询无座余票
 if(text == "有" or text == "无"):
  wztickets_now = text
  print("%s 无座 : %s"%(train_num, wztickets_now))
  if(wztickets_now != wztickets_last):
   wztickets_last = wztickets_now
   send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last)
 else:
  wztickets_now = text
  print("%s 无座剩余票数 : %s"%(train_num, wztickets_now))
  if(wztickets_now != wztickets_last):
   wztickets_last = wztickets_now
   send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last)
 print("")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/Meringue_/article/details/79207241

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部