1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #!/usr/bin/env python3 # -*- coding: utf-8 -*-
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time
class monitorTickets(): """ 查询12306硬座,硬卧,软卧余票 """ def __init__(self): """初始化""" #使用chrome浏览器打开查询12306查询页面 self.browser = webdriver.Chrome() url = 'https://kyfw.12306.cn/otn/leftTicket/init' self.browser.get(url)
def search_tickets(self,train_name, start_stations, end_stations, ticket_id): """循环获取余票""" print("开始查询【%s】次列车余票信息" % train_name) for start_station in start_stations: try: for end_station in end_stations: #输入起点 starts = self.browser.find_element_by_id("fromStationText") starts.click() starts.clear() starts.send_keys(start_station) starts.send_keys(Keys.ENTER)
#输入到站 ends = self.browser.find_element_by_id("toStationText") ends.click() ends.clear() ends.send_keys(end_station) ends.send_keys(Keys.ENTER)
#选择时间 #使用js将只读属性去除,完成选择时间 js = 'document.getElementById("train_date").removeAttribute("readonly");' self.browser.execute_script(js) date = self.browser.find_element_by_id('train_date') date.clear() date.send_keys('2018-02-10') date.click()
self.browser.find_element_by_id("query_ticket").click() time.sleep(2)
text = self.browser.find_element_by_id(ticket_id).text if text == u"无": print(u"【%s】到【%s】的余票为0,节哀~" % (start_station, end_station)) time.sleep(1) elif text == u"*": print(u"【%s】到【%s】的车票还未开售,请耐心等待~" % (start_station, end_station)) time.sleep(1) else: print(u"发现【%s】到【%s】的余票,抢!" % (start_station, end_station)) except: continue print("结束查询【%s】次列车余票信息" % train_name)
def close_browser(self): """关浏览器""" time.sleep(3) self.browser.quit()
if __name__ == '__main__': #查票 time.sleep(1) search_ticket = monitorTickets() while True: print("监控硬座") search_ticket.search_tickets('T116', ['上海', '苏州'], ['宝鸡', '天水', '陇西'], 'YZ_550000T11671') search_ticket.search_tickets('Z216', ['上海', '苏州'], ['宝鸡', '天水', '甘谷'], 'YZ_550000Z21605') search_ticket.search_tickets('T112', ['嘉兴', '昆山', '苏州'], ['宝鸡', '天水', '陇西'], 'YZ_560000T11251') search_ticket.search_tickets('T204', ['上海', '昆山', '苏州'], ['宝鸡', '天水', '兰州'], 'YZ_550000T20450') search_ticket.search_tickets('Z40', ['上海', '苏州'], ['宝鸡', '天水', '兰州'], 'YZ_5500000Z4061') print("监控硬座结束")
# print("监控硬卧") # search_ticket.search_tickets('T116', ['上海', '苏州'], ['宝鸡', '天水', '陇西'], 'YW_550000T11671') # search_ticket.search_tickets('Z216', ['上海', '苏州'], ['宝鸡', '天水', '甘谷'], 'YW_550000Z21605') # search_ticket.search_tickets('T112', ['嘉兴', '昆山', '苏州'], ['宝鸡', '天水', '陇西'], 'YW_560000T11251') # search_ticket.search_tickets('T204', ['上海', '昆山', '苏州'], ['宝鸡', '天水', '兰州'], 'YW_550000T20450') # search_ticket.search_tickets('Z40', ['上海', '苏州'], ['宝鸡', '天水', '兰州'], 'YW_5500000Z4061') # print("监控硬卧结束") # # print("监控软卧") # search_ticket.search_tickets('T116', ['上海', '苏州'], ['宝鸡', '天水', '陇西'], 'RW_550000T11671') # search_ticket.search_tickets('Z216', ['上海', '苏州'], ['宝鸡', '天水', '甘谷'], 'RW_550000Z21605') # search_ticket.search_tickets('T112', ['嘉兴', '昆山', '苏州'], ['宝鸡', '天水', '陇西'], 'RW_560000T11251') # search_ticket.search_tickets('T204', ['上海', '昆山', '苏州'], ['宝鸡', '天水', '兰州'], 'RW_550000T20450') # search_ticket.search_tickets('Z40', ['上海', '苏州'], ['宝鸡', '天水', '兰州'], 'RW_5500000Z4061') # print("监控软卧结束") # msg = input("Enter 'q' to quit") # if msg == 'q': # break # search_ticket.close_browser()
|