12306爬虫抢票软件代码

'''

1、需要登录页面,手动输入账号密码!
2、地查询车次,输入  起点、目的、出发时间,找到车次,就进行选择
3、选择完成后,就查询该车次是否有余票(进行显示)如果有就进行购买,没有就进行循环
4、如果有就进入购买界面,找到 或者 输入 乘车人信息 ,提交
5、点击提交按钮,付款完成抢票


https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E5%8C%97%E4%BA%AC,BJP&ts=%E4%B8%8A%E6%B5%B7,SHH&date=2021-05-19&flag=N,N,Y
https://kyfw.12306.cn/otn/confirmPassenger/initDc
'''
import time
import requests
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
from selenium.webdriver.common.keys import Keys
import lxml
import re








class Qiangpiao(object):
    def __init__(self,text_):
        self.login_url = 'https://kyfw.12306.cn/otn/resources/login.html'           #登录页面
        self.inter_url = 'https://kyfw.12306.cn/otn/view/index.html'                #信息界面
        self.option_url = 'https://kyfw.12306.cn/otn/leftTicket/init'               #选择页面
        #self.display_url = 'https://kyfw.12306.cn/otn/leftTicket/init'              #显示所有车次的页面
        self.purchase_url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'     #预定付款界面
        self.driver = webdriver.Chrome('F:\python\PyCharm Community Edition 2020.3.3\\bin\chromedriver.exe')# 驱动chrome浏览器进行操作

    def wait_input(self):
        self.from_station = "北京"#input('出发地:')
        self.to_station = "上海"#input('目的地:')
        # 时间格式一定要对应
        self.depart_time = "2021-06-07"#input('出发时间:')
        # 名字一定要存在于常用联系人中间
        self.passengers = "张三"#input('乘客姓名:(如有多个乘客使用英文逗号分割)').split(',')
        self.trains = ["G101"] #input('车次:(如有多个车次使用英文逗号分割)').split(',')  # 结果[G234,...]
    def login(self):
        self.driver.get(self.login_url)    #判断是否进入了登录界面
        WebDriverWait(self.driver, 1000).until(EC.url_to_be(self.inter_url))
        #EC.url_to_be(url)判断是否是 url 这个网页,必须一致,如果是返回True,否则返回False
        #driver:浏览器驱动,1000是最长超时时间(timeout:最长超时时间,默认以秒为单位)
        #登录账号密码
        print('恭喜您,您已登录成功了!')

    def dataGain(self):
        self.driver.find_element_by_id("fromStationText").click()
        fromStationText = self.driver.find_element_by_id("fromStationText")
        fromStationText.send_keys(Keys.BACK_SPACE)
        fromStationText.send_keys("北京")
        fromStationText = self.driver.find_element_by_id("fromStationText")
        fromStationText.send_keys(Keys.ENTER)

        #安照输入需要哪些步骤,就需要执行哪些代码

        self.driver.find_element_by_id("toStationText").click()
        toStationText = self.driver.find_element_by_id("toStationText")
        toStationText.send_keys(Keys.BACK_SPACE)
        toStationText.send_keys("上海")
        toStationText.send_keys(Keys.ENTER)

        #出发时间
        x = True
        while x == True :
            a = 5 #int(input("请输入0-16之间的数字:"))
            if 0 <= a < 16:
                to_time = self.driver.find_elements(By.XPATH,'//*[@id="date_range"]/ul/li['+str(a)+']')#这个XPATH是按钮整个外结构的XPATH
                to_time[0].click()
                x = False
            else:
                print("输入有误,请重新输入!!")
        #to_time[0].send_keys(Keys.ENTER)
        time.sleep(5)

    def _order_ticket(self):
        self.driver.get(self.option_url)
        sear = self.driver.find_element_by_id("gb_closeDefaultWarningWindowDialog_id").click()
        #self.driver.find_element_by_id("fromStationText").send_keys("北京")
        #se = self.driver.find_element_by_id("fromStation_icon_image").click()       #点击所要输入的地址,先爬取到所有的地址
        self.dataGain()

        #self.driver.find_element_by_xpath(' //*[@id="ticket_240000G1010Q_01_11"]/td[13]').click()                                         # 获取到点击事件
        #找到点击按钮,执行点击事件train
        time.sleep(5)
        '''
        trains_list = self.driver.find_elements_by_class_name("number")       #车次列表
        start_list = self.driver.find_elements_by_class_name("start-s")       #出发站
        end_list = self.driver.find_elements_by_class_name("end-s")           #终点站
        start_end_time_list = self.driver.find_elements_by_class_name("cds")  #出发时间和到达时间
        last_list = self.driver.find_elements_by_class_name("ls")             #历时
        business_list = self.driver.find_elements_by_class_name("t-num")      #商务座的票
        you_list = self.driver.find_elements_by_class_name("yes")             #一等座
        in_class = self.driver.find_elements_by_class_name("bgc")
        '''
        # 需要等待信息加载......
        self.driver.find_elements(By.ID, 'queryLeftTable')
        # 需要找到tr标签进行加载信息
        tr_list = self.driver.find_elements_by_xpath('//*[@id="queryLeftTable"]/tr[not(@datatran)]')    #获取到了所有的标签框,里面有所有的车次
        start_list = self.driver.find_elements_by_class_name("cdz")           #出发站和终点站  ok
        start_end_time_list = self.driver.find_elements_by_class_name("cds")  # 出发时间和到达时间  ok
        last_list = self.driver.find_elements_by_class_name("ls")  # 历时         ok
        left_ticket1 = self.driver.find_elements_by_xpath(".//td[2]")[1:]   #商务座的名额剩余,切片操作一下,第一个不是所需要的内容
        left_ticket2 = self.driver.find_elements_by_xpath(".//td[3]")   #一等座的名额剩余
        left_ticket3 = self.driver.find_elements_by_xpath(".//td[4]")   #二等座的名额剩余
        left_ticket4 = self.driver.find_elements_by_xpath(".//td[5]")   #高级软卧的名额剩余
        left_ticket5 = self.driver.find_elements_by_xpath(".//td[6]")   #软卧一等卧的名额剩余
        left_ticket6 = self.driver.find_elements_by_xpath(".//td[7]")   #动卧的名额剩余
        left_ticket7 = self.driver.find_elements_by_xpath(".//td[8]")   #硬卧二等卧的名额剩余
        left_ticket8 = self.driver.find_elements_by_xpath(".//td[9]")   #软座的名额剩余
        left_ticket9 = self.driver.find_elements_by_xpath(".//td[10]")   #硬座的名额剩余
        left_ticket10 = self.driver.find_elements_by_xpath(".//td[11]") #无座的名额剩余
        left_ticket11 = self.driver.find_elements_by_xpath(".//td[12]") #其他的名额剩余
        x = 0
        # 打印出所有的标签
        tplt1 = "{0:<0}\t{1:^9}\t{2:^9}\t{3:^9}\t{4:^9}\t{5:^9}\t{6:^9}\t{7:^9}\t{8:^9}\t{9:^9}\t{10:^5}\t{11:^9}\t{12:^9}\t{13:^9}\t{14:^9}"
        for tr in tr_list:
            x += 1
        print(tplt1.format("编号", "车次", "出发站和终点站", "出发时间和到达时间", "历时", "商务座", "一等座", "二等座", "高等软卧", "软卧一等座", "动卧", "硬卧", "硬座", "无座", "其他", chr(12288)))
        tplt2 = "{0:<0}\t{1:^9}\t{2:^9}\t{3:^9}\t{4:^9}\t{5:^9}\t{6:^9}\t{7:^9}\t{8:^9}\t{9:^9}\t{10:^9}\t{11:^9}\t{12:^9}\t{13:^9}\t{14:^9}"
        for j in range(0,x):
            tr_text = tr_list[j].find_element_by_class_name("number")
            print(tplt2.format(j, tr_text.text,start_list[j].text.replace("\n","到"),start_end_time_list[j].text.replace("\n","—"),last_list[j].text.replace("\n","——"),left_ticket1[j].text,left_ticket2[j].text,left_ticket3[j].text,left_ticket4[j].text,left_ticket5[j].text,left_ticket6[j].text,left_ticket7[j].text,left_ticket8[j].text,left_ticket9[j].text,left_ticket10[j].text,left_ticket11[j].text,chr(12288)))

        z = 5 #int(input("请输入需要查询的车次序号:"))
        #找到那几个标签的内容判断是否有票
        print(tplt1.format("编号", "车次", "出发站和终点站", "出发时间和到达时间", "历时", "商务座", "一等座", "二等座", "高等软卧", "软卧一等座", "动卧", "硬卧",
                          "硬座", "无座", "其他", chr(12288)))
        for j in range(0, x):
            tr_text = tr_list[j].find_element_by_class_name("number")
            if z == j:
                print(tplt2.format(j, tr_text.text,start_list[j].text.replace("\n","到"),start_end_time_list[j].text.replace("\n","—"),last_list[j].text.replace("\n","——"),left_ticket1[j].text,left_ticket2[j].text,left_ticket3[j].text,left_ticket4[j].text,left_ticket5[j].text,left_ticket6[j].text,left_ticket7[j].text,left_ticket8[j].text,left_ticket9[j].text,left_ticket10[j].text,left_ticket11[j].text,chr(12288)))
                left_ticket12 = self.driver.find_elements_by_xpath(".//td[13]")[j].click()
                if WebDriverWait(self.driver, 1000).until(EC.url_to_be(self.purchase_url)) == True:
                    print("进入预付款界面,请输入信息\n")
                    inter = self.driver.find_element_by_class_name("lay-bd")        #获取页面基础信息
                    print(inter.text,"\n")
                    users = self.driver.find_element_by_class_name("per-sel")       #获取所有的人员信息
                    print(users.text,end='')
                    print("\n")

                    nu = 1
                    k = 10
                    while nu == 1:
                        print("0 受让人,乘车人从 1 开始")
                        i = int(input())
                        if i == 0:
                            self.driver.find_element_by_class_name("btn92s").click()
                            break
                        elif i == 1:
                            check = self.driver.find_element_by_class_name("check")
                            check[i].click()
                            self.driver.find_element_by_id("btn92s").click()
                        elif i > 1:
                            check = self.driver.find_element_by_class_name("check")
                            check[i].click()
                            print(self.driver.find_element_by_class_name("tit").text)
                            self.driver.find_element_by_class_name("gb_closeDefaultWarningWindowDialog_id").click()
                        #print("是否继续添加:是:1,否:0")
                        #nu = int(input())


                    #获取所有的车次预约按钮

        '''click_list = self.driver.find_elements_by_class_name("btn72")

        self.driver.find_elements_by_id("tr_id_1")
        self.driver.find_elements_by_xpath(".//td[1]")


        for i,j,k in zip(trains_list,start_list,end_list):
            x += 1
            print(i.text," ",j.text," ",k.text,x)
            print("==============")
            print("=========================================================")
        x = 0
        for l in in_class:
            x += 1
            print(l.text,x)'''
        '''
        x = 0
        for i in business_list:
            x += 1
            print(i.text, x)
            print("==============")
        print("=========================================================")
        x = 0
        for i in you_list:
            x += 1
            print(i.text, x)
            print("==============")
'''


    def run(self):
        self.wait_input()
        self.login()
        self._order_ticket()

if __name__ == '__main__':
    spider = Qiangpiao(text_=None)
    spider.run()
    #spider.wait_input()
    #spider.login()
    #spider._order_ticket()















你可能感兴趣的:(爬虫,python,开发语言)