您好,登錄后才能下訂單哦!
今天就和大家一起來(lái)討論一下python實(shí)現(xiàn)12306余票查詢(pycharm+python3.7),一起來(lái)感受一下python爬蟲的簡(jiǎn)單實(shí)踐
我們說(shuō)先在瀏覽器中打開開發(fā)者工具(F12),嘗試一次余票的查詢,通過(guò)開發(fā)者工具查看發(fā)出請(qǐng)求的包
可以看到紅框框中的URL就是我們向12306服務(wù)器發(fā)出的請(qǐng)求,那么具體是什么呢?我們來(lái)看看
https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2019-01-21&leftTicketDTO.from_station=CDW&leftTicketDTO.to_station=SZQ&purpose_codes=ADULT
可以看到發(fā)出請(qǐng)求的幾個(gè)字段:
可以從我們遞交的URL請(qǐng)求看出,我們輸入的成都,深圳都變成了對(duì)應(yīng)的編號(hào),比如,成都(CDW)、深圳(SZQ),所以當(dāng)我們程序進(jìn)行輸入的時(shí)候要進(jìn)行一下處理,12306的一個(gè)地方存儲(chǔ)著這些城市名與編碼對(duì)應(yīng)的文檔:
https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.8971
下面我們就編寫一個(gè)小程序,將這些城市名與編號(hào)提取出來(lái):
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) #將車站的名字和編碼進(jìn)行提取 chezhan = re.findall(r'([\u4e00-\u9fa5]+)\|([A-Z]+)', response.text) chezhan_code = dict(chezhan) #進(jìn)行交換 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': '長(zhǎng)春', 'CET': '長(zhǎng)春南', 'CRT': '長(zhǎng)春西', 'ICW': '成都東', 'CNW': '成都南', 'CDW': '成都', 'CSQ': '長(zhǎng)沙', 'CWQ': '長(zhǎng)沙南',}
接下來(lái)我們就動(dòng)手開始程序的主要代碼編寫:
def main(): date = input("請(qǐng)輸入時(shí)間(如2019-01-22):\n") from_station = chezhan_code[input("請(qǐng)輸入起始站點(diǎn):\n")] to_station = chezhan_code[input("請(qǐng)輸入目的站點(diǎn):\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)檢查過(guò)生成的URL是正確的 #request請(qǐng)求獲取主頁(yè) r = requests.get(url,headers=headers) r.raise_for_status() #如果發(fā)送了一個(gè)錯(cuò)誤的請(qǐng)求,會(huì)拋出異常 r.encoding = r.apparent_encoding showTicket(r.text)
用戶輸入時(shí)間、起始站點(diǎn)、目的站點(diǎn),然后通過(guò)get來(lái)請(qǐng)求,然后我們對(duì)返回的網(wǎng)頁(yè)信息進(jìn)行解析。我們現(xiàn)將上面代碼的r.text進(jìn)行打印,看看我們請(qǐng)求之后,返回了什么樣的信息,然后決定我們應(yīng)該如何解析
這樣看著不方便,我們粘貼到記事本中,進(jìn)行詳細(xì)的分析:
可以與12306顯示的信息進(jìn)行對(duì)比,K829是車次,CDW與BJQ是出發(fā)地和目的地,10:10是出發(fā)時(shí)間,06:13是到達(dá)時(shí)間,44:21是歷時(shí)時(shí)間,20190123為查詢的日期,剩下的就是一系列票的各種信息。
下面就是對(duì)這些返回的信息進(jìn)行解析,其實(shí)這也是python爬蟲的關(guān)鍵,就是解析?。?!
我們先把信息轉(zhuǎn)化為json格式,可以看到都是用“|”隔開的,那么我們就用split函數(shù)分割出來(lái),下面是主要功能代碼:
def showTicket(html): html = json.loads(html) table = PrettyTable([" 車次 ","出發(fā)車站","到達(dá)車站","出發(fā)時(shí)間","到達(dá)時(shí)間"," 歷時(shí) ","商務(wù)座"," 一等座","二等座","高級(jí)軟臥","軟臥","動(dòng)臥","硬臥","軟座","硬座","無(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": '' } #將各項(xiàng)信息提取并賦值 item = i.split('|') #使用“|”進(jìn)行分割 data["station_train_code"] = item[3] #獲取車次信息,在3號(hào)位置 data["from_station_name"] = item[6] #始發(fā)站信息在6號(hào)位置 data["to_station_name"] = item[7] #終點(diǎn)站信息在7號(hào)位置 data["start_time"] = item[8] #出發(fā)時(shí)間在8號(hào)位置 data["arrive_time"] = item[9] #抵達(dá)時(shí)間在9號(hào)位置 data["lishi"] = item[10] #經(jīng)歷時(shí)間在10號(hào)位置 data["swz_num"] = item[32] or item[25] #特別注意,商務(wù)座在32或25位置 data["zy_num"] = item[31] #一等座信息在31號(hào)位置 data["ze_num"] = item[30] #二等座信息在30號(hào)位置 data["gr_num"] = item[21] #高級(jí)軟臥信息在21號(hào)位置 data["rw_num"] = item[23] #軟臥信息在23號(hào)位置 data["dw_num"] = item[27] #動(dòng)臥信息在27號(hào)位置 data["yw_num"] = item[28] #硬臥信息在28號(hào)位置 data["rz_num"] = item[24] #軟座信息在24號(hào)位置 data["yz_num"] = item[29] #硬座信息在29號(hào)位置 data["wz_num"] = item[26] #無(wú)座信息在26號(hào)位置 data["qt_num"] = item[22] #其他信息在22號(hào)位置 data["note_num"] = item[1] #備注信息在1號(hào)位置 color = Colored() data["note_num"] = color.white(item[1]) #如果沒(méi)有信息,那么就用“-”代替 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)
那么我們程序就成功啦!?。?/p>
但是在編譯器里面Prettytable的格子沒(méi)有對(duì)齊,不要擔(dān)心,我們到終端運(yùn)行一下腳本,就可以看到很好看的輸出啦:
完成?。?!下面是完整代碼
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ā)車站","到達(dá)車站","出發(fā)時(shí)間","到達(dá)時(shí)間"," 歷時(shí) ","商務(wù)座"," 一等座","二等座","高級(jí)軟臥","軟臥","動(dòng)臥","硬臥","軟座","硬座","無(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": '' } #將各項(xiàng)信息提取并賦值 item = i.split('|') #使用“|”進(jìn)行分割 data["station_train_code"] = item[3] #獲取車次信息,在3號(hào)位置 data["from_station_name"] = item[6] #始發(fā)站信息在6號(hào)位置 data["to_station_name"] = item[7] #終點(diǎn)站信息在7號(hào)位置 data["start_time"] = item[8] #出發(fā)時(shí)間在8號(hào)位置 data["arrive_time"] = item[9] #抵達(dá)時(shí)間在9號(hào)位置 data["lishi"] = item[10] #經(jīng)歷時(shí)間在10號(hào)位置 data["swz_num"] = item[32] or item[25] #特別注意,商務(wù)座在32或25位置 data["zy_num"] = item[31] #一等座信息在31號(hào)位置 data["ze_num"] = item[30] #二等座信息在30號(hào)位置 data["gr_num"] = item[21] #高級(jí)軟臥信息在21號(hào)位置 data["rw_num"] = item[23] #軟臥信息在23號(hào)位置 data["dw_num"] = item[27] #動(dòng)臥信息在27號(hào)位置 data["yw_num"] = item[28] #硬臥信息在28號(hào)位置 data["rz_num"] = item[24] #軟座信息在24號(hào)位置 data["yz_num"] = item[29] #硬座信息在29號(hào)位置 data["wz_num"] = item[26] #無(wú)座信息在26號(hào)位置 data["qt_num"] = item[22] #其他信息在22號(hào)位置 data["note_num"] = item[1] #備注信息在1號(hào)位置 color = Colored() data["note_num"] = color.white(item[1]) #如果沒(méi)有信息,那么就用“-”代替 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("請(qǐng)輸入時(shí)間:\n") from_station = chezhan_code[input("請(qǐng)輸入起始站點(diǎn):\n")] to_station = chezhan_code[input("請(qǐng)輸入目的站點(diǎn):\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)檢查過(guò)生成的URL是正確的 #request請(qǐng)求獲取主頁(yè) r = requests.get(url,headers=headers) r.raise_for_status() #如果發(fā)送了一個(gè)錯(cuò)誤的請(qǐng)求,會(huì)拋出異常 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) #將車站的名字和編碼進(jìn)行提取 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)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。