python實現(xiàn)12306余票查詢
我們說先在瀏覽器中打開開發(fā)者工具(F12),嘗試一次余票的查詢,通過開發(fā)者工具查看發(fā)出請求的包
余票查詢界面
可以看到紅框框中的URL就是我們向12306服務(wù)器發(fā)出的請求,那么具體是什么呢?我們來看看
[
https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2019-01-21leftTicketDTO.from_station=CDWleftTicketDTO.to_station=SZQpurpose_codes=ADULT](https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2019-01-21leftTicketDTO.from_station=CDWleftTicketDTO.to_station=SZQpurpose_codes=ADULT)
可以看到發(fā)出請求的幾個字段:
leftTicketDTO.train_date:查詢的日期
leftTicketDTO.from_station:查詢的出發(fā)地
leftTicketDTO.to_station:查詢的目的地
purpose_codes:不太清楚這個字段是用來做什么的,就默認吧
可以從我們遞交的URL請求看出,我們輸入的成都,深圳都變成了對應的編號,比如,成都(CDW)、深圳(SZQ),所以當我們程序進行輸入的時候要進行一下處理,12306的一個地方存儲著這些城市名與編碼對應的文檔:
[
https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8971](https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8971)
站點編碼對應
下面我們就編寫一個小程序,將這些城市名與編號提取出來:
import re,requests
url = "https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8971"
response = requests.get(url,verify=False)
#將車站的名字和編碼進行提取
chezhan = re.findall(r'([\u4e00-\u9fa5]+)\|([A-Z]+)', response.text)
chezhan_code = dict(chezhan)
#進行交換
chezhan_names = dict(zip(chezhan_code.values(),chezhan_code.keys()))
#打印出得到的車站字典
print(chezhan_names)
得到的打印結(jié)果如下(只截取部分顯示):
{‘VAP': ‘北京北', ‘BOP': ‘北京東', ‘BJP': ‘北京', ‘VNP': ‘北京南', ‘BXP': ‘北京西', ‘IZQ':
‘廣州南', ‘CUW': ‘重慶北', ‘CQW': ‘重慶', ‘CRW': ‘重慶南', ‘CXW': ‘重慶西', ‘GGQ': ‘廣州東',
‘SHH': ‘上海', ‘SNH': ‘上海南', ‘AOH': ‘上海虹橋', ‘SXH': ‘上海西', ‘TBP': ‘天津北', ‘TJP':
‘天津', ‘TIP': ‘天津南', ‘TXP': ‘天津西', ‘XJA': ‘香港西九龍', ‘CCT': ‘長春', ‘CET': ‘長春南',
‘CRT': ‘長春西', ‘ICW': ‘成都東', ‘CNW': ‘成都南', ‘CDW': ‘成都', ‘CSQ': ‘長沙', ‘CWQ':
‘長沙南',}
接下來我們就動手開始程序的主要代碼編寫:
def main():
date = input("請輸入時間(如2019-01-22):\n")
from_station = chezhan_code[input("請輸入起始站點:\n")]
to_station = chezhan_code[input("請輸入目的站點:\n")]
url = "https://kyfw.12306.cn/otn/leftTicket/queryZ?"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5702.400 QQBrowser/10.2.1893.400"
}
url=url+"leftTicketDTO.train_date="+date+"leftTicketDTO.from_station="+from_station+"leftTicketDTO.to_station="+to_station+"purpose_codes=ADULT"
#print(url) 已經(jīng)檢查過生成的URL是正確的
#request請求獲取主頁
r = requests.get(url,headers=headers)
r.raise_for_status() #如果發(fā)送了一個錯誤的請求,會拋出異常
r.encoding = r.apparent_encoding
showTicket(r.text)
用戶輸入時間、起始站點、目的站點,然后通過get來請求,然后我們對返回的網(wǎng)頁信息進行解析。我們現(xiàn)將上面代碼的r.text進行打印,看看我們請求之后,返回了什么樣的信息,然后決定我們應該如何解析
運行結(jié)果
這樣看著不方便,我們粘貼到記事本中,進行詳細的分析:
請求返回的結(jié)果信息
可以與12306顯示的信息進行對比,K829是車次,CDW與BJQ是出發(fā)地和目的地,10:10是出發(fā)時間,06:13是到達時間,44:21是歷時時間,20190123為查詢的日期,剩下的就是一系列票的各種信息。
下面就是對這些返回的信息進行解析,其實這也是python爬蟲的關(guān)鍵,就是解析?。?!
我們先把信息轉(zhuǎn)化為json格式,可以看到都是用“|”隔開的,那么我們就用split函數(shù)分割出來,下面是主要功能代碼:
def showTicket(html):
html = json.loads(html)
table = PrettyTable([" 車次 ","出發(fā)車站","到達車站","出發(fā)時間","到達時間"," 歷時 ","商務(wù)座"," 一等座","二等座","高級軟臥","軟臥","動臥","硬臥","軟座","硬座","無座","其他","備注"])
for i in html['data']['result']:
name = [
"station_train_code",
"from_station_name",
"to_station_name",
"start_time",
"arrive_time",
"lishi",
"swz_num",
"zy_num",
"ze_num",
"dw_num",
"gr_num",
"rw_num",
"yw_num",
"rz_num",
"yz_num",
"wz_num",
"qt_num",
"note_num"
]
data = {
"station_train_code": '',
"from_station_name": '',
"to_station_name": '',
"start_time": '',
"arrive_time": '',
"lishi": '',
"swz_num": '',
"zy_num": '',
"ze_num": '',
"dw_num": '',
"gr_num": '',
"rw_num": '',
"yw_num": '',
"rz_num": '',
"yz_num": '',
"wz_num": '',
"qt_num": '',
"note_num": ''
}
#將各項信息提取并賦值
item = i.split('|') #使用“|”進行分割
data["station_train_code"] = item[3] #獲取車次信息,在3號位置
data["from_station_name"] = item[6] #始發(fā)站信息在6號位置
data["to_station_name"] = item[7] #終點站信息在7號位置
data["start_time"] = item[8] #出發(fā)時間在8號位置
data["arrive_time"] = item[9] #抵達時間在9號位置
data["lishi"] = item[10] #經(jīng)歷時間在10號位置
data["swz_num"] = item[32] or item[25] #特別注意,商務(wù)座在32或25位置
data["zy_num"] = item[31] #一等座信息在31號位置
data["ze_num"] = item[30] #二等座信息在30號位置
data["gr_num"] = item[21] #高級軟臥信息在21號位置
data["rw_num"] = item[23] #軟臥信息在23號位置
data["dw_num"] = item[27] #動臥信息在27號位置
data["yw_num"] = item[28] #硬臥信息在28號位置
data["rz_num"] = item[24] #軟座信息在24號位置
data["yz_num"] = item[29] #硬座信息在29號位置
data["wz_num"] = item[26] #無座信息在26號位置
data["qt_num"] = item[22] #其他信息在22號位置
data["note_num"] = item[1] #備注信息在1號位置
color = Colored()
data["note_num"] = color.white(item[1])
#如果沒有信息,那么就用“-”代替
for pos in name:
if data[pos] == "":
data[pos] = "-"
tickets = []
cont = []
cont.append(data)
for x in cont:
tmp = []
for y in name:
if y == "from_station_name":
s = color.green(chezhan_names[data["from_station_name"]])
tmp.append(s)
elif y == "to_station_name":
s = color.red(chezhan_names[data["to_station_name"]])
tmp.append(s)
elif y == "start_time":
s = color.green(data["start_time"])
tmp.append(s)
elif y == "arrive_time":
s = color.red(data["arrive_time"])
tmp.append(s)
elif y == "station_train_code":
s = color.yellow(data["station_train_code"])
tmp.append(s)
else:
tmp.append(data[y])
tickets.append(tmp)
for ticket in tickets:
table.add_row(ticket)
print(table)
那么我們程序就成功啦?。?!
運行結(jié)果
但是在編譯器里面Prettytable的格子沒有對齊,不要擔心,我們到終端運行一下腳本,就可以看到很好看的輸出啦:
終端運行結(jié)果
完成?。?!下面是完整代碼
main.py
# -*- coding: utf-8 -*-
import re,requests,datetime,time,json
from prettytable import PrettyTable
from colorama import init,Fore
from stationinfo import chezhan_code,chezhan_names
init(autoreset=False)
class Colored(object):
def yeah(self,s):
return Fore.LIGHTCYAN_EX + s + Fore.RESET
def green(self,s):
return Fore.LIGHTGREEN_EX + s + Fore.RESET
def yellow(self,s):
return Fore.LIGHTYELLOW_EX + s + Fore.RESET
def white(self,s):
return Fore.LIGHTWHITE_EX + s + Fore.RESET
def blue(self,s):
return Fore.LIGHTBLUE_EX + s + Fore.RESET
def showTicket(html):
html = json.loads(html)
table = PrettyTable([" 車次 ","出發(fā)車站","到達車站","出發(fā)時間","到達時間"," 歷時 ","商務(wù)座"," 一等座","二等座","高級軟臥","軟臥","動臥","硬臥","軟座","硬座","無座","其他","備注"])
for i in html['data']['result']:
name = [
"station_train_code",
"from_station_name",
"to_station_name",
"start_time",
"arrive_time",
"lishi",
"swz_num",
"zy_num",
"ze_num",
"dw_num",
"gr_num",
"rw_num",
"yw_num",
"rz_num",
"yz_num",
"wz_num",
"qt_num",
"note_num"
]
data = {
"station_train_code": '',
"from_station_name": '',
"to_station_name": '',
"start_time": '',
"arrive_time": '',
"lishi": '',
"swz_num": '',
"zy_num": '',
"ze_num": '',
"dw_num": '',
"gr_num": '',
"rw_num": '',
"yw_num": '',
"rz_num": '',
"yz_num": '',
"wz_num": '',
"qt_num": '',
"note_num": ''
}
#將各項信息提取并賦值
item = i.split('|') #使用“|”進行分割
data["station_train_code"] = item[3] #獲取車次信息,在3號位置
data["from_station_name"] = item[6] #始發(fā)站信息在6號位置
data["to_station_name"] = item[7] #終點站信息在7號位置
data["start_time"] = item[8] #出發(fā)時間在8號位置
data["arrive_time"] = item[9] #抵達時間在9號位置
data["lishi"] = item[10] #經(jīng)歷時間在10號位置
data["swz_num"] = item[32] or item[25] #特別注意,商務(wù)座在32或25位置
data["zy_num"] = item[31] #一等座信息在31號位置
data["ze_num"] = item[30] #二等座信息在30號位置
data["gr_num"] = item[21] #高級軟臥信息在21號位置
data["rw_num"] = item[23] #軟臥信息在23號位置
data["dw_num"] = item[27] #動臥信息在27號位置
data["yw_num"] = item[28] #硬臥信息在28號位置
data["rz_num"] = item[24] #軟座信息在24號位置
data["yz_num"] = item[29] #硬座信息在29號位置
data["wz_num"] = item[26] #無座信息在26號位置
data["qt_num"] = item[22] #其他信息在22號位置
data["note_num"] = item[1] #備注信息在1號位置
color = Colored()
data["note_num"] = color.white(item[1])
#如果沒有信息,那么就用“-”代替
for pos in name:
if data[pos] == "":
data[pos] = "-"
tickets = []
cont = []
cont.append(data)
for x in cont:
tmp = []
for y in name:
if y == "from_station_name":
s = color.green(chezhan_names[data["from_station_name"]])
tmp.append(s)
elif y == "to_station_name":
s = color.yeah(chezhan_names[data["to_station_name"]])
tmp.append(s)
elif y == "start_time":
s = color.green(data["start_time"])
tmp.append(s)
elif y == "arrive_time":
s = color.yeah(data["arrive_time"])
tmp.append(s)
elif y == "station_train_code":
s = color.yellow(data["station_train_code"])
tmp.append(s)
else:
tmp.append(data[y])
tickets.append(tmp)
for ticket in tickets:
table.add_row(ticket)
print(table)
def main():
date = input("請輸入時間:\n")
from_station = chezhan_code[input("請輸入起始站點:\n")]
to_station = chezhan_code[input("請輸入目的站點:\n")]
url = "https://kyfw.12306.cn/otn/leftTicket/queryZ?"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5702.400 QQBrowser/10.2.1893.400"
}
url=url+"leftTicketDTO.train_date="+date+"leftTicketDTO.from_station="+from_station+"leftTicketDTO.to_station="+to_station+"purpose_codes=ADULT"
#print(url) 已經(jīng)檢查過生成的URL是正確的
#request請求獲取主頁
r = requests.get(url,headers=headers)
r.raise_for_status() #如果發(fā)送了一個錯誤的請求,會拋出異常
r.encoding = r.apparent_encoding
showTicket(r.text)
#print(r.text)
main()
stationinfo.py
import re,requests
url = "https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8971"
response = requests.get(url,verify=False)
#將車站的名字和編碼進行提取
chezhan = re.findall(r'([\u4e00-\u9fa5]+)\|([A-Z]+)', response.text)
chezhan_code = dict(chezhan)
chezhan_names = dict(zip(chezhan_code.values(),chezhan_code.keys()))
#print(chezhan_names)
到此這篇關(guān)于教你用python實現(xiàn)12306余票查詢的文章就介紹到這了,更多相關(guān)python實現(xiàn)12306余票查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python多線程實現(xiàn)模擬火車站售票
- Python自動化xpath實現(xiàn)自動搶票搶貨
- 春節(jié)到了 教你使用python來搶票回家
- 用Python搶火車票的簡單小程序?qū)崿F(xiàn)解析
- 為了順利買到演唱會的票用Python制作了自動搶票的腳本