python爬虫实现12306抢票

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python爬虫实现12306抢票

馆主阿牛   2022-05-23 我要评论

1.环境

python 3.7
谷歌浏览器
chromedriver.exe(浏览器驱动程序,要与浏览器版本对应,并将其添加到环境变量或放到当前py文件所在目录下)

2.相关模块

time (用于某些地方对程序的强制等待)
datatime (用于获取当前时间)
selenium 3.1 自动化测试模块,这里用于操作浏览器)

3.思路

首先进行登录(支持手机扫码),登录完成进入页面之后,我们会看到有“温馨提示”的弹窗,即当前界面,我们需要处理第一次弹窗,

处理完后就是信息的输入,在这里日期的输入不是输入框,是鼠标点击,在代码中已处理,输入完成后,查询列车信息(我写的只能查询动车以及火车的二等座),如果有票,则预约,然后进入新的页面

处理乘车人信息(由于本人是学生,此代码也可抢学生票),最后提交订单。
其实说明白了,就是通过代码让浏览器模拟人来操作浏览器,从而实现购票,要求要对selenium这个模块掌握的熟练。不说了,直接上代码,量有点大,各位道友一定要撑住哈。

import time
import datetime
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Qiangpiao():
    def __init__(self,from_station,to_station,depart_time,train_num,passenger):
        self.login_url = 'https://kyfw.12306.cn/otn/resources/login.html'
        self.init_my_url = 'https://kyfw.12306.cn/otn/view/index.html'
        self.order_url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'
        # input("出发地:")
        self.from_station = from_station
        # input("目的地:")
        self.to_station = to_station
        # 时间格式必须是M-d的方式
        # input("出发时间(格式必须是M-d的方式):")
        self.depart_time = depart_time
        # input("列车号:")
        self.train_num = train_num
        self.passenger = passenger
        #获取当前月份
        self.now_month = datetime.date.today().month

        self.leave_month = int(self.depart_time.split('-')[0])
        self.leave_day = int(self.depart_time.split('-')[1])

        self.driver = webdriver.Chrome()

    def _login(self):
        self.driver.get(self.login_url)
        # 窗口最大化
        #self.driver.maximize_window()
        # 设置窗口大小
        self.driver.set_window_size(1300,800)
        #print('调整前尺寸:', self.driver.get_window_size())
        #显式等待
        #这里进行手动登录,可以扫码,也可以输入账号密码点击登录
        WebDriverWait(self.driver,1000).until(EC.url_to_be(self.init_my_url))
        print('登录成功!')

    def _pop_window(self):
        time.sleep(1)
        self.driver.find_element_by_xpath('//*[@class="dzp-confirm"]/div[2]/div[3]/a').click()

    def _enter_order_ticket(self):
        action = ActionChains(self.driver)   # 实例化一个动作链对象
        element = self.driver.find_element_by_link_text('车票')
        # 鼠标移动到 '车票' 元素上的中心点
        action.move_to_element(element).perform()
        # 点击'单程'
        self.driver.find_element_by_xpath('//*[@id="J-chepiao"]/div/div[1]/ul/li[1]/a').click()
        # 消除第二次弹窗
        self.driver.find_element_by_link_text('确认').click()

    def _search_ticket(self):
        #出发地输入
        self.driver.find_element_by_id("fromStationText").click()
        self.driver.find_element_by_id("fromStationText").send_keys(self.from_station)
        self.driver.find_element_by_id("fromStationText").send_keys(Keys.ENTER)
        #目的地输入
        self.driver.find_element_by_id("toStationText").click()
        self.driver.find_element_by_id("toStationText").send_keys(self.to_station)
        self.driver.find_element_by_id("toStationText").send_keys(Keys.ENTER)
        #出发日期输入
        self.driver.find_element_by_id("date_icon_1").click()
        if self.leave_month == self.now_month:
            xpath_str = f"/html/body/div[37]/div[1]/div[2]/div[{self.leave_day}]"
            if EC.element_to_be_clickable((By.XPATH, xpath_str)):
                self.driver.find_element_by_xpath(xpath_str).click()
            else:
                print("当前日期未到或已过售票日期,无法购票!")
        elif self.leave_month == self.now_month + 1:
            xpath_str = f"/html/body/div[37]/div[2]/div[2]/div[{self.leave_day}]"
            if EC.element_to_be_clickable((By.XPATH, xpath_str)):
                self.driver.find_element_by_xpath(xpath_str).click()
            else:
                print("当前日期未到或已过售票日期,无法购票!")
        else:
            print("月份超前一个月以上,无法购票!")
        #等待查询按钮是否可用
        WebDriverWait(self.driver,1000).until(EC.element_to_be_clickable((By.ID,"query_ticket")))
        #执行点击事件
        search_btn = self.driver.find_element_by_id("query_ticket")
        search_btn.click()
        #等待查票信息加载
        WebDriverWait(self.driver, 1000).until(EC.presence_of_element_located((By.XPATH, '//*[@id="queryLeftTable"]/tr')))

    def _order_ticket(self):
        train_num_list = []  # 列车号列表
        train_num_ele_list = self.driver.find_elements_by_xpath('//tr/td[1]/div/div[1]/div/a')  # 列车号元素列表
        for t in train_num_ele_list:    # 遍历列车号元素列表,并把列车号添加到列车号列表
            train_num_list.append(t.text)
        tr_list = self.driver.find_elements_by_xpath('//*[@id="queryLeftTable"]/tr[not(@datatran)]')  #每一列列车整行信息列表,列车号元素是tr的子元素
        if self.train_num in train_num_list:
            for tr in tr_list:
                train_num = tr.find_element_by_xpath("./td[1]/div/div[1]/div/a").text  #取出元素tr里的列车号
                if self.train_num == train_num:
                    #动车二等座余票信息
                    text_1 = tr.find_element_by_xpath("./td[4]").text
                    # 火车二等座余票信息
                    text_2 = tr.find_element_by_xpath("./td[8]").text
                    if (text_1 == "有" or text_1.isdigit()) or (text_2 == "有" or text_2.isdigit()):
                        #点击预订按钮
                        order_btn = tr.find_element_by_class_name("btn72")
                        order_btn.click()
                        #等待订票页面
                        WebDriverWait(self.driver,1000).until(EC.url_to_be(self.order_url))
                        # 选定乘车人
                        self.driver.find_element_by_xpath(f'//*[@id="normal_passenger_id"]/li/label[contains(text(),"{self.passenger}")]').click()
                        #如果乘客是学生,对提示点击确定
                        if EC.presence_of_element_located((By.XPATH, '//div[@id="dialog_xsertcj"]')):
                            self.driver.find_element_by_id('dialog_xsertcj_ok').click()
                            # 提交订单
                            self.driver.find_element_by_id('submitOrder_id').click()
                            time.sleep(2)
                            # 点击确认订单
                            self.driver.find_element_by_id('qr_submit_id').click()
                        else:
                            # 提交订单
                            self.driver.find_element_by_id('submitOrder_id').click()
                            time.sleep(2)
                            # 点击确认
                            self.driver.find_element_by_id('qr_submit_id').click()
                            print("购票成功!")
                            break
                    else:
                        print("二等座无票!")
        else:
            print("无此列车!")

    def run(self):
        #登录
        self._login()
        #消除登录后(第一次)的弹窗
        self._pop_window()
        #进入购票页面
        self._enter_order_ticket()
        #查票
        self._search_ticket()
        #订票
        self._order_ticket()
        #关闭浏览器
        time.sleep(6)
        self.driver.quit()

if __name__ == '__main__':
    qiangpiao = Qiangpiao("兰州","乌鲁木齐","8-6","D55","小红")
    qiangpiao.run()

4.结语

看到这里你们有么有崩溃,哈哈哈,事实上作为一名计算机系的大学生,我们一定要脚踏实地,多实践多敲代码,切记,在学习过程中一定要自己动手敲,这个世界运行的底层逻辑不是白piao,是价值捆绑,作为程序员要多思考思考底层逻辑,形成闭环思维。

同时你们是否有疑惑,这程序运行一次怎么抢票,事实上代码运行是比手操作快的,而且,你完全可以设置一个for循环,然后设置一个时间(time)间隔,让其每隔一段时间运行一次,直到抢票成功时停止,让其运行个几天,这样票刚一发售,程序会立马抢到,从而实现真正意义上的抢票,同时,还有许多内容可以完善,比如座位种类,乘客多选,以及做模拟登录然后隐藏浏览器(无头浏览器了解一下)等等,有兴趣的可以试试哦!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们