2023-09-15 pyhton 抢票

import sys
from time import sleep
from selenium.webdriver.support.ui import Select
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import smtplib
from email.mime.text import MIMEText
from email.header import Header

class TrainSpider(object):
    def __init__(self):
        #登陆url
        self.url = 'https://kyfw.12306.cn/otn/login/init'
        #个人主页
        self.self_url = 'https://kyfw.12306.cn/otn/view/index.html'
        #购买车票url
        self.buy_url = 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc'
        #创建浏览器对象
        self.browser = webdriver.Chrome()
        #12306账号
        self.username = ''
        #12306密码
        self.password = ''
        #出发站
        self.begin = ''
        #终点站
        self.end = ''
        # 出发时间 注意: 如果是1-9月份,前面需要加上0,如:01,02
        self.begin_time = ''
        #乘车人
        self.name = ''
        # 坐车时间(0:全天, 1:0点-6点, 2:6点-12点, 3:12点-18点, 4:18-24点)
        self.go_time = ''
        # 第三方 SMTP 服务  设置服务器
        self.mail_host = ""
        # 用户名
        self.mail_user = ""
        # 邮箱授权码,不是密码
        self.mail_pass = ""
        # 邮件发送人
        self.sender = ''
        # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
        self.receivers = ['']

    def login(self):
        # 设置浏览器全屏
        self.browser.maximize_window()
        self.browser.get(self.url)
        # 输入用户名
        sleep(0.5)
        self.browser.find_element_by_id('username').send_keys(self.username)
        #输入密码
        self.browser.find_element_by_id('password').send_keys(self.password)
        print(u"等待验证码,自行输入...")
        # 检测验证码是否输入正确
        while True:
            if self.browser.current_url!= self.self_url:
                sleep(1)
            else:
                break
        print('登陆成功')
        sleep(1)
        #调用查询车票函数
        self.query_ticket()

    def query_ticket(self):
        self.browser.get(self.buy_url)
        sleep(0.2)
        #输入出发车站
        self.browser.find_element_by_id('fromStationText').click()
        self.browser.find_element_by_id('fromStationText').send_keys(self.begin,Keys.ENTER)
        #输入终点站
        self.browser.find_element_by_id('toStationText').click()
        self.browser.find_element_by_id('toStationText').send_keys(self.end,Keys.ENTER)

        # 网页中日期js代码,更改为可手动输入
        js = 'document.getElementById("train_date").removeAttribute("readonly");'
        self.browser.execute_script(js)
        #全选默认日期
        self.browser.find_element_by_id("train_date").send_keys(Keys.CONTROL, "a")
        #删除默认日期
        self.browser.find_element_by_id("train_date").send_keys(Keys.DELETE)
        #输入日期
        self.browser.find_element_by_id('train_date').send_keys(self.begin_time,Keys.ENTER)
        #选择乘车时间段
        Select(self.browser.find_element_by_id('cc_start_time')).select_by_index(self.go_time)
        self.browser.find_element_by_id('query_ticket').click()
        self.select_ticket()
        sleep(1)

    #获取车票信息
    def select_ticket(self):
        count = 0
        while self.browser.current_url == self.buy_url:
            self.browser.find_element_by_id('query_ticket').click()
            sleep(2)
            train_list = self.browser.find_elements_by_class_name('no-br')
            count += 1
            print("循环点击查询... 第 %s 次" % count)
            if train_list == []:
                continue
            else:
                for t in train_list:
                    t.click()
                    self.buy_train()

    #模拟点击火车票下单
    def buy_train(self):
        sleep(3)
        passanger_labels = self.browser.find_elements_by_xpath(".//ul[@id = 'normal_passenger_id']/li/label")
        for passanger_label in passanger_labels:
            name = passanger_label.text
            sleep(0.5)
            if name == self.name:
                passanger_label.click()
                sleep(2)
                self.browser.find_element_by_id('submitOrder_id').click()
                sleep(2)
                self.browser.find_element_by_id('qr_submit_id').click()
                print('下单成功,请手动付款!!!')
                self.e_mail()
                sys.exit()
            else:
                print('请等待...')

    # 邮件发送
    def e_mail(self):
        message = MIMEText('车票下单成功,请手动付款', 'plain', 'utf-8')
        message['From'] = Header("12306", 'utf-8')
        message['To'] = Header("Python", 'utf-8')
        subject = '车票购买'
        message['Subject'] = Header(subject, 'utf-8')
        try:
            smtpObj = smtplib.SMTP()
            smtpObj.connect(self.mail_host, 25)  # 25 为 SMTP 端口号
            smtpObj.login(self.mail_user, self.mail_pass)
            smtpObj.sendmail(self.sender, self.receivers, message.as_string())
            print("邮件发送成功")
        except Exception as e:
            print("Error: 无法发送邮件", e)

if __name__ == '__main__':
    spider = TrainSpider()
    spider.login()

你可能感兴趣的:(2023-09-15 pyhton 抢票)