# -*- coding: utf-8 -*-
from splinter.browser import Browser
from time import sleep
import traceback
from twilio.rest import Client
# import sys
# reload(sys)
# sys.setdefaultencoding('utf-8') class Buy_Tickets(object):
# 定义实例属性,初始化
def __init__(self, username, passwd, order, passengers, dtime, starts, ends):
self.username = username
self.passwd = passwd
# 车次,0代表所有车次,1代表第一个车次,依次从上到下,依次类推
self.order = order
# 乘客名
self.passengers = passengers
# 起始地
self.starts = starts
# 目的点
self.ends = ends
# 日期
self.dtime = dtime
# self.xb = xb
# self.pz = pz
self.login_url = 'https://kyfw.12306.cn/otn/login/init'
self.initMy_url = 'https://kyfw.12306.cn/otn/view/index.html'
self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init'
self.buy_url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'
self.driver_name = 'chrome'
self.executable_path = 'E:\Python\Scripts\chromedriver.exe'
def login(self):
self.driver.visit(self.login_url)
self.driver.fill('loginUserDTO.user_name', self.username)
self.driver.fill('userDTO.password', self.passwd)
print('输入验证码...')
while 1:
if self.driver.url != self.initMy_url:
sleep(5)
else:
break
def start_buy(self):
self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path)
#窗口大小的操作
self.driver.driver.set_window_size(1000, 700)
self.login()
self.driver.visit(self.ticket_url)
try:
self.check_ticket()
self.buy_ticket()
except Exception as e:
print(e)
def check_ticket(self):
print('开始选票...')
# 加载查询信息
self.driver.cookies.add({"_jc_save_fromStation": self.starts})
self.driver.cookies.add({"_jc_save_toStation": self.ends})
self.driver.cookies.add({"_jc_save_fromDate": self.dtime})
self.driver.reload()
count = 0
if self.order != 0:
while self.driver.url == self.ticket_url:
self.driver.find_by_text('查询').click()
count += 1
print('第 %d次点击查询...' % count)
try:
self.driver.find_by_text('预订')[self.order-1].click()
sleep(1.5)
except Exception as e:
print(e)
print('预订失败...')
continue
else:
while self.driver.url == self.ticket_url:
self.driver.find_by_text('查询').click()
count += 1
print('第%d次点击查询...' % count)
try:
for i in self.driver.find_by_text('预订'):
if i.has_class('btn72'):
i.click()
sleep(1)
except Exception as e:
print(e)
print('预订失败...')
continue
sleep(1)
def buy_ticket(self):
print('开始预订...')
sleep(1)
print('选择用户...')
for p in self.passengers:
self.driver.find_by_text(p).last.click()
sleep(0.5)
if p[-1] == ')':
self.driver.find_by_id('dialog_xsertcj_ok').click()
print('提交订单...')
# sleep(1)
# self.driver.find_by_text(self.pz).click()
# sleep(1)
# self.driver.find_by_text(self.xb).click()
# sleep(1)
self.driver.find_by_id('submitOrder_id').click()
sleep(2)
print('确认选座...')
self.driver.find_by_id('qr_submit_id').click()
print('预订成功...')
sme_text = '抢到票了,赶紧去付款 !!\n I get a ticket,now going to pay !!'
self.sendSMS(my_number='xxx',msg=sme_text)
print '短信发送成功'
def sendSMS(self,my_number,msg='你好,这是来自你自己的手机测试信息!'):
account_sid = 'xxx'
auth_token = 'xxx'
twilio_number = 'xxx'
client = Client(account_sid, auth_token)
message = client.messages.create(from_=twilio_number,body=msg,to=my_number)
print(message.sid) if __name__ == '__main__':
# 用户名
username = 'xxx'
# 密码
password = 'xxx'
# 车次选择,0代表所有车次
order = 0
# 乘客名,学生票需注明,比如passengers = ['小红', '小明(学生)']
passengers = ['xxx']
# 日期
dtime = '2019-02-01'
# 出发地(cookie值)
starts = '%u5357%u4EAC%u5357%2CNKH' #南京南
# 目的地(cookie值)
ends = '%u6C49%u53E3%2CHKN' #汉口 # xb =['硬座']
# pz=['成人票']
Buy_Tickets(username, password, order, passengers, dtime, starts, ends).start_buy()

下载 chromedriver.exe,镜像网址  http://npm.taobao.org/mirrors/chromedriver/

发送短信,申请token,   https://www.twilio.com/

python抢火车票 短信通知的更多相关文章

  1. python抢火车票的脚本

    起因: 想着那么多人,抢不到火车票.感觉到一丝感慨 所以有了抢火车票这个脚本. 0x01 思路:自动打开浏览器,自动输入账号密码 知道查看.自动预定. 0x02 要用到的模块 splinter模块: ...

  2. Python用上锁和解锁 lock lock.acquire lock.release 模拟抢火车票

    Python用上锁和解锁  lock lock.acquire lock.release 模拟抢火车票 import jsonimport timefrom multiprocessing impor ...

  3. 用Python抢到回家的车票,so easy!

    “ 盼望着,盼望着,春节的脚步近了,然而,每年到这个时候,最难的,莫过于一张回家的火车票. ​ 据悉,今年春运期间,全国铁路发送旅客人次同比将增长 8.0%.达到 4.4 亿人次. ​ 2020 年铁 ...

  4. python自动刷新抢火车票

    #!/usr/bin/env python #-*- coding: utf-8 -*- """ 火车票 可以自动填充账号密码,同时,在登录时,也可以修改账号密码 然后手 ...

  5. 史上最“脑残”的“抢火车票”程序(node.js版)

    [背景] 快过年了,我妈一个电话打过来叫我给他买火车票,我到12306一查,硬座和硬卧基本没有了,高铁又太贵. 最后只抢了3张无座票,但是我妈说能不能买有座位的啊,我说没有了啊,我妈:你过两天再帮我看 ...

  6. python抢小米6自动化脚本

    #!/bin/env python # coding=utf-8 from selenium import webdriver import time import unittest class Ge ...

  7. Python 实现火车票查询工具

    注意:由于 12306 的接口经常变化,课程内容可能很快过期,如果遇到接口问题,需要根据最新的接口对代码进行适当修改才可以完成实验. 一.实验简介 当你想查询一下火车票信息的时候,你还在上 12306 ...

  8. python抢票开发——设备预约助手实现

    女朋友是药学院的,做实验时需要在特定的网站上进行设备预约,由于预约人数过多,从而导致从浏览器登录不进去或者登录进去预约失败等情况,所以我用python帮她写了一个抢位助手,让程序自动去进行位置预定,实 ...

  9. GitHub 上有哪些优秀的 Python 爬虫项目?

    目录 GitHub 上有哪些优秀的 Python 爬虫项目? 大型爬虫项目: 实用型爬虫项目: 其它有趣的Python爬虫小项目: GitHub 上有哪些优秀的 Python 爬虫项目? 大型爬虫项目 ...

随机推荐

  1. leetCode26.删除排序数组中的重复项

    给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...

  2. how find out what is causing Visual Studio to think each project is out of date

    You can find out what is causing Visual Studio to think each project is out of date, and then addres ...

  3. dubbo使用简介

    ---------------------------------------------------------------------------------------------------- ...

  4. nginx 返回数据不完整

    当nginx 代理解析大量数据流时,会把数据先放在自己的缓冲区,然后一并发给客户端 一次请求的数据量很大, 则会有一部分数据会被忽略掉 前端解析数据会有问题 致使页面白屏 nginx 返回数据不完整的 ...

  5. SpringMVC 请求全过程漫谈

    SpringMVC 请求全过程漫谈 SpringMVC 跟其他的mvc框架一样,如 struts,webwork, 本质上都是 将一个 http 请求(request)进行各种处理, 然后返回resp ...

  6. leetcode1030

    class Solution(object): def __init__(self): self.List = list() def bfs(self,R,C,S,V): T = list() whi ...

  7. 002之MFCSocket异步编程

    当今的网络程序通用体系结构大多为C/S模式,服务器监听收到来自客户端的请求,然后响应并作出应答. 界面对话框如下,输入IP信息进行通信后再进行连接,连接成功即可开始通信.左侧为客户端,右侧为服务端. ...

  8. jQuery获取URL中的参数

    //获取URL地址栏中的参数 function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + &quo ...

  9. 一些hue的参考网址

    CDH5.8 HUE的使用(那些年走过的坑) https://blog.csdn.net/gao123456789amy/article/details/79242713 HUE的时间问题等 http ...

  10. SpringMVC处理XML格式的数据

    1.搭建SpringMVC+spring环境 2.web.xml,Springmvc-config.xml.springMVC提供了处理xml格式请求响应的HttpMessageConverter,s ...