12306抢票爬虫selenium+Chromedriver(需手动完成支付)

  1. 先看动态结果:只能到预定锁定车票,30分钟内手动付款,车票会被自动取消。

    如果是暂无余票,就会一直查询余票信息,直到有余票时,自动预定,比如G7暂无余票,为了防止反爬虫,设置的刷票间隔时间长一点,这样更安全。

     

  2. 使用selenium+chromedriver工具,自动查询余票,和订票,如果暂时无票,程序会一直不停的查询余票情况。
  3. 首先需要输入出发城市,到达城市,和乘车日期,车次,乘车人等信息,然后登陆,实现自动购票。
  4. 不足之处:不能同时查询,多个车次,正在尝试选择解决办法。
  5. 代码如下
    # project:  12306
    # author:  liulei
    # time:  2019/8/20 8:44
    # file:  qiangpiao1.py
    import random
    import time
    
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC #期望的条件
    from selenium.webdriver.common.by import By
    
    
    class Qiangpiao(object):
        def __init__(self):
            self.login_url = 'https://kyfw.12306.cn/otn/login/init'
            self.initmy_url = 'https://kyfw.12306.cn/otn/view/index.html'
            # self.initmy_url = 'https://kyfw.12306.cn/otn/index/initMy12306'
            self.search_url = 'https://kyfw.12306.cn/otn/leftTicket/init'
            self.confirmPassenger = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'
            self.driver = webdriver.Chrome(executable_path=r'C:\chromedriver\chromedriver.exe') #驱动chrome浏览器进行操作
    
        def wait_input(self):
            self.from_station = input('出发地:')
            self.to_station = input('目的地:')        
            self.depart_time = input('出发日期:')     
            self.passengers = input('乘客姓名(多名乘客请用英文逗号隔开):').split(',')
            self.trains = input('车次:').split(',') 
    
        def _login(self):
            self.driver.get(self.login_url) #打开登录界面
            WebDriverWait(self.driver,1000).until(EC.url_to_be(self.initmy_url))
            print('登录成功!')
    
        def _order_ticket(self):       
            self.driver.get(self.search_url)
            WebDriverWait(self.driver, 1000).until(EC.text_to_be_present_in_element_value((By.ID, "fromStationText"), self.from_station))
            WebDriverWait(self.driver, 1000).until(EC.text_to_be_present_in_element_value((By.ID, "toStationText"), self.to_station))
            WebDriverWait(self.driver, 1000).until(EC.text_to_be_present_in_element_value((By.ID, "train_date"), self.depart_time))
            WebDriverWait(self.driver, 1000).until(EC.element_to_be_clickable((By.ID, "query_ticket")))
            searchBotton = self.driver.find_element_by_id("query_ticket")
            searchBotton.click()
            WebDriverWait(self.driver, 1000).until(EC.presence_of_element_located((By.XPATH,".//tbody[@id = 'queryLeftTable']/tr")))
            tr_list = self.driver.find_elements_by_xpath(".//tbody[@id ='queryLeftTable']/tr[not(@datatran)]")
            for tr in tr_list:
                train_number = tr.find_element_by_class_name('number').text
                if train_number in self.trains:
                    left_ticket = tr.find_element_by_xpath('.//td[4]').text #找到二等座
                    # 如果暂时无票,抢票(循环不停的刷新余票)
                    count = 1
                    while left_ticket == '无' or left_ticket == '--' or left_ticket =='候补':
                        print("暂无余票,正在尝试第%s次抢票···" % count)
                        time.sleep(random.randint(300, 1000))
    
                        searchBotton.click()
                        count += 1
    
                    if left_ticket == '有' or left_ticket.isdigit: #判断输入的车次是否在列表中
                        orderBotton = tr.find_element_by_class_name('btn72')
                        orderBotton.click()
    
                        WebDriverWait(self.driver, 1000).until(EC.url_to_be(self.confirmPassenger))
                        WebDriverWait(self.driver, 1000).until(EC.presence_of_element_located((By.XPATH,".//ul[@id = 'normal_passenger_id']/li")))
                        passanger_labels = self.driver.find_elements_by_xpath(".//ul[@id = 'normal_passenger_id']/li/label")
                        for passanger_label in passanger_labels: 
                            name = passanger_label.text
                            if name in self.passengers:
                                time.sleep(1)
                                passanger_label.click() 
                        submitBotton = self.driver.find_element_by_id('submitOrder_id')
                        time.sleep(1)
                        submitBotton.click()
                        WebDriverWait(self.driver, 1000).until(EC.presence_of_element_located((By.CLASS_NAME,'dhtmlx_wins_body_outer')))
                        WebDriverWait(self.driver, 1000).until(EC.presence_of_element_located((By.ID,'qr_submit_id')))
                        ConBotton = self.driver.find_element_by_id('qr_submit_id')
                        time.sleep(1)
                        ConBotton.click()
                        print("车票预定成功,请手动完成支付!")
                        print("系统正常退出……")
                        exit()
    
        def run(self):
            self.wait_input()
            self._login()
            self._order_ticket()
    
    
    if __name__ == '__main__':
        spider = Qiangpiao()
        spider.run()
    
    
    

     

你可能感兴趣的:(Python爬虫)