您好,登錄后才能下訂單哦!
這篇文章主要介紹python如何實現自動查詢12306余票并發送郵箱提醒腳本,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
腳本使用了python + selenium + chrome webdriver,需要先配置好三者的環境。
具體使用時候,需自行改變代碼中的
郵箱信息、出發站、到達站、車次、日期、以及到相應查詢頁面中找到對應車票的html id:
在相應查詢頁面中按F12,彈出開發者模式,點擊按鈕,再點擊對應車票的框,,,復制相應的id號到代碼中對應處。
代碼如下:
#coding:utf8 from selenium import webdriver from selenium.webdriver.common.keys import Keys import time from smtplib import SMTP_SSL from email.header import Header from email.mime.text import MIMEText from_station = 'SH' to_station = 'NJ' train_num = 'G14' train_date = '2018-02-14' ZYid = 'ZY_5l00000G1441' #通過瀏覽器找到要查詢車票的三個id ZEid = 'ZE_5l00000G1441' WZid = 'WZ_5l00000G1441' def send_email(train_date, train_num, one_num, two_num, wz_num): #函數中郵箱信息改為自己的 email_from = "123456@qq.com" email_to = "654321@qq.com" hostname = "smtp.qq.com" login = "123456@qq.com" password = "xddflwjfkkfkbidf" subject = "train_tickets" text = ("日期:%s 車次:%s 一等座:%s 二等座:%s 無座:%s "%(train_date, train_num, one_num, two_num, wz_num)) smtp = SMTP_SSL(hostname) smtp.login(login, password) msg = MIMEText(text, "plain", "utf-8") msg["Subject"] = Header(subject, "utf-8") msg["from"] = email_from msg["to"] = email_to smtp.sendmail(email_from, email_to, msg.as_string()) smtp.quit() driver = webdriver.Chrome() driver.get('https://kyfw.12306.cn/otn/index/init') elem = driver.find_element_by_id("fromStationText") elem.clear() elem.send_keys(from_station) elem.send_keys(Keys.ENTER) #寫入始發站 elem = driver.find_element_by_id("toStationText") elem.clear() elem.send_keys(to_station) elem.send_keys(Keys.ENTER) #寫入終點站 js = "document.getElementById('train_date').removeAttribute('readonly')" #去除日期欄只讀屬性 driver.execute_script(js) elem=driver.find_element_by_id("train_date") elem.clear() elem.send_keys(train_date) #寫入乘車日期 elem.click() elem=driver.find_element_by_id("dc") #選擇單程 elem.click() elem=driver.find_element_by_id("a_search_ticket") #開始查詢 try: elem.click() except Exception: time.sleep(2) query_times = 0 onetickets_last = 0 twotickets_last = 0 wztickets_last = 0 time.sleep(2) while(1): query_times = query_times+1 text="" try: driver.find_element_by_id("query_ticket").click() #根據查詢鍵是否可以找到,以判斷頁面顯示正確與否 except Exception: driver.refresh() time.sleep(5); print("bug"); continue print("第%d次查詢:"%(query_times)) time.sleep(5) try: text = driver.find_element_by_id(ZYid).text except Exception: driver.refresh() time.sleep(5); print("bug") continue if not(driver.find_element_by_id(ZYid).text): driver.find_element_by_id("query_ticket").click() text = driver.find_element_by_id(ZYid).text #查詢一等座余票 if(text == "有" or text == "無"): onetickets_now = text print("%s 一等座 : %s"%(train_num, onetickets_now)) if(onetickets_now != onetickets_last): onetickets_last = onetickets_now #send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last) else: onetickets_now = text print("%s 一等座剩余票數 : %s"%(train_num, onetickets_now)) if(onetickets_now != onetickets_last): onetickets_last = onetickets_now #send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last) text = driver.find_element_by_id(ZEid).text #查詢二等座余票 if(text == "有" or text == "無"): twotickets_now = text print("%s 二等座 : %s"%(train_num, twotickets_now)) if(twotickets_now != twotickets_last): twotickets_last = twotickets_now send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last) else: twotickets_now = text print("%s 二等座剩余票數 : %s"%(train_num,twotickets_now)) if(twotickets_now != twoticketstwotickets_old): twotickets_last = twotickets_now send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last) text = driver.find_element_by_id(WZid).text #查詢無座余票 if(text == "有" or text == "無"): wztickets_now = text print("%s 無座 : %s"%(train_num, wztickets_now)) if(wztickets_now != wztickets_last): wztickets_last = wztickets_now send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last) else: wztickets_now = text print("%s 無座剩余票數 : %s"%(train_num, wztickets_now)) if(wztickets_now != wztickets_last): wztickets_last = wztickets_now send_email(train_date, train_num, onetickets_last, twotickets_last, wztickets_last) print("")
以上是“python如何實現自動查詢12306余票并發送郵箱提醒腳本”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。