以selenium模擬登陸12306
阿新 • • 發佈:2019-03-30
comm return n) ati RoCE fin 當前 div not
初級模擬,速度有點慢,後期有時間再優化
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 importBy 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()
以selenium模擬登陸12306