• python抢火车票 短信通知


    # -*- 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:PythonScriptschromedriver.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 = '抢到票了,赶紧去付款 !!
     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/ 

  • 相关阅读:
    Gin编写图床后端上传图片代码实现
    Kubernetes的ETCD集群备份、恢复
    Linux虚拟机(lvm)报Unmount and run xfs_repair
    Pod长时处于init状态
    netstat命令查看网络连接各状态的连接数
    WARNING: HiveonMR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
    启动azkaban服务加载不到钉钉报警机器人的插件
    Mac系统查看maven版本时出现The JAVA_HOME environment variable is not defined correctly, this environment variable is needed to run this program.
    Plugin 'mavenassemblyplugin:' not found
    钉钉机器人发送消息出现{"errcode":310000,"errmsg":"keywords not in content
  • 原文地址:https://www.cnblogs.com/jhin-wxy/p/10246663.html
Copyright © 2020-2023  润新知