以selenium模拟登陆12306

 初级模拟,速度有点慢,后期有时间再优化

  1 # -*- coding:utf-8 -*-
  2 # author:zxy
  3 # date:2018-12-23
  4 
  5 from selenium import webdriver
  6 from selenium.webdriver.support.ui import WebDriverWait
  7 from selenium.webdriver.support import expected_conditions as EC
  8 from selenium.webdriver.common.by import By
  9 from http.cookiejar import CookieJar
 10 from urllib import request
 11 from urllib import parse
 12 
 13 
 14 class login_12306():
 15     def get_opener(self):
 16         # 1登录
 17         # 1.1 创建一个CookieJar对象
 18         self.cookiejar = CookieJar()
 19         # 1.2 使用cookiejar创建一个HTTPCookieProcessor()对象
 20         self.handler = request.HTTPCookieProcessor(self.cookiejar)
 21         # 1.3 使用上一步创建的handler创建一个opener
 22         opener = request.build_opener(self.handler)
 23         return opener
 24 
 25 class ticketGradding():
 26 
 27     def __init__(self):
 28         self.driver_path = r"E:\chromDriver\chromedriver.exe"
 29         self.initMy_url = "https://kyfw.12306.cn/otn/view/index.html"
 30         self.driver = webdriver.Chrome(executable_path=self.driver_path)
 31         self.login_url = "https://kyfw.12306.cn/otn/login/init"
 32         self.ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init"
 33         self.confirm_url = "https://kyfw.12306.cn/otn/confirmPassenger/initDc"
 34 
 35     def _wait_input(self):
 36         self.from_station = input("起始站")
 37         self.to_station = input("目的地")
 38 
 39         # 时间格式  yyyy-MM-dd
 40         self.depart_time = input("出发时间:")
 41         self.passengers = input("乘客姓名(如有多个乘客,用英文逗号隔开):").split(",")
 42         self.trains = input("车次(如果多个车次,用英文逗号隔开):").split(",")
 43 
 44     def _login(self):  # 函数名前加_,表示该函数只能在当前类使用
 45         self.driver.get(self.login_url)
 46         # 显示等待
 47         # 隐示等待
 48         WebDriverWait(self.driver, 1000).until(
 49             EC.url_to_be(self.initMy_url)
 50         )
 51         print("登陆成功")
 52 
 53     def _order_ticket(self):
 54         # 跳转到查余票的界面
 55         self.driver.get(self.ticket_url)
 56         self.tickets = []
 57         # 等待出发地是否输入正确
 58         WebDriverWait(self.driver, 1000).until(
 59 
 60             # 元组
 61             EC.text_to_be_present_in_element_value((By.ID, "fromStationText"), self.from_station)
 62         )
 63 
 64         # 等待目的地时候输入正确
 65         WebDriverWait(self.driver, 1000).until(
 66             EC.text_to_be_present_in_element_value((By.ID, "toStationText"), self.to_station)
 67         )
 68 
 69         # 等待出发时间是否输入正确
 70         WebDriverWait(self.driver, 1000).until(
 71             EC.text_to_be_present_in_element_value((By.ID, "train_date"), self.depart_time)
 72         )
 73 
 74         # 等待查询按钮是否可用
 75         WebDriverWait(self.driver, 1000).until(
 76             EC.element_to_be_clickable((By.ID, 'query_ticket'))
 77         )
 78 
 79         # 执行查询事件
 80         searchBtn = self.driver.find_element_by_id('query_ticket')
 81         searchBtn.click()
 82 
 83         # 等待显示车次信息
 84         WebDriverWait(self.driver, 1000).until(
 85             EC.presence_of_element_located((By.XPATH, ".//tbody[@id='queryLeftTable']/tr"))
 86         )
 87 
 88         tr_list = self.driver.find_element_by_xpath(".//tbody[@id='queryLeftTable']/tr[not (@datatrain)]")
 89 
 90         for tr in tr_list:
 91             train_number = tr.find_element_by_class_name("number").text
 92             if train_number in self.trains:
 93                 best_ticket = tr.find_element_by_xpath(".//td[2]").text
 94                 first_ticket = tr.find_element_by_xpath(".//td[3]").text
 95                 second_ticket = tr.find_element_by_xpath(".//td[4]").text
 96                 super_soft_ticket = tr.find_element_by_xpath(".//td[5]").text
 97                 soft_lay_ticket = tr.find_element_by_xpath(".//td[6]").text
 98                 move_lay_ticket = tr.find_element_by_xpath(".//td[7]").text
 99                 hard_lay_ticket = tr.find_element_by_xpath(".//td[8]").text
100                 sort_sit_ticket = tr.find_element_by_xpath(".//td[9]").text
101                 hard_sit_ticket = tr.find_element_by_xpath(".//td[10]").text
102                 no_sit_ticket = tr.find_element_by_xpath(".//td[11]").text
103                 others = tr.find_element_by_xpath(".//td[12]").text
104                 # self.tickets.append(best_ticket,first_ticket,second_ticket,super_soft_ticket,soft_lay_ticket,\
105                 #                     move_lay_ticket,hard_lay_ticket,sort_sit_ticket,hard_sit_ticket,\
106                 #                     no_sit_ticket,others)
107                 orderBtn = tr.find_element_by_class_name("btn72")
108                 if best_ticket.isdigit or best_ticket == "":
109                     print(train_number + "商务座特等座有票,序号为0")
110                     # orderBtn.click()
111                 elif first_ticket.isdigit or first_ticket == "":
112                     print(train_number + "一等座有票,序号为1")
113                     # orderBtn.click()
114                 elif second_ticket.isdigit or second_ticket == "":
115                     print(train_number + "二等座有票,序号为2")
116                     # orderBtn.click()
117                 elif super_soft_ticket.isdigit or super_soft_ticket == "":
118                     print(train_number + "三等座有票,序号为3")
119                     # orderBtn.click()
120                 elif soft_lay_ticket.isdigit or soft_lay_ticket == "":
121                     print(train_number + "高级软卧有票,序号为4")
122                 elif move_lay_ticket.isdigit or move_lay_ticket == "":
123                     print(train_number + "软卧有票,序号为5")
124                 elif hard_lay_ticket.isdigit or hard_lay_ticket == "":
125                     print(train_number + "动卧有票,序号为6")
126                 elif sort_sit_ticket.isdigit or sort_sit_ticket == "":
127                     print(train_number + "软座有票,序号为7")
128                 elif hard_sit_ticket.isdigit or hard_sit_ticket == "":
129                     print(train_number + "硬座有票,序号为8")
130                 elif no_sit_ticket.isdigit or no_sit_ticket == "":
131                     print(train_number + "无座有票,序号9")
132                 elif others.isdigit or others == "":
133                     print(train_number + "其他有票,序号10")
134 
135                 choice = input("请输入有票的且座位等级对应的编号:")
136                 if (choice):
137                     orderBtn.click()
138 
139                     # 确认选票页面
140                     WebDriverWait(self.driver, 1000).until(
141                         EC.url_to_be(self.confirm_url)
142                     )
143 
144     def run(self):
145         self._wait_input()
146         self._login()
147         self._order_ticket()
148 
149 
150 if __name__ == '__main__':
151     spider = ticketGradding()
152     spider.run()

 

posted @ 2019-03-30 08:51  浅忆~  阅读(662)  评论(0编辑  收藏  举报