由于车票难抢,有时需要的车票已经售空,而我们需要捡漏,便可使用这个脚本。
具体实现了,自动查询某一车票的余票数量,当数量产生变化时,将自动发送QQ邮件到对于的邮箱进行提醒。
其中,发送邮箱的部分详情可参考我上一篇博文:Python 发送邮件 (smtp) QQ邮箱
脚本使用了python + selenium + chrome webdriver,需要先配置好三者的环境。
具体使用时候,需自行改变代码中的
邮箱信息、出发站、到达站、车次、日期、以及到相应查询页面中找到对应车票的html id:
在相应查询页面中按F12,弹出开发者模式,点击按钮,再点击对应车票的框,,,复制相应的id号到代码中对应处。
代码如下:
#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("")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“python自动查询12306余票并发送邮箱提醒脚本”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新动态
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]