Python查詢12306餘票
一、requests等庫的介紹
requests:傳送請求,獲取響應內容
docopt:命令列解析工具,可以根據自定義的語法格式,自動生成解析器
prettytable:使用表格形式列印資料
colorama:命令列著色
二、開發環境搭建
下載Python2的安裝檔案[windows版]
安裝Python2
此處省略N個字
配置環境變數
修改環境變數Path,追加Python2的安裝路徑[C:\Python27]

image
檢查環境是否正常

image
給eclipse工具安裝Aptana外掛

image
配置eclipse
設定python直譯器的路徑[C:\Python27\python.exe]

image
三、安裝相關的庫
安裝requests庫

image
安裝docopt庫
pip install docopt
安裝prettytable庫
pip install prettytable
安裝colorama庫
pip install colorama
四、建立一個python工程
在eclipse裡面建立一個python工程,如下圖:

image
五、設計一個字典stations
stations字典裡面的資料為:站點和對應編碼
程式碼如下:
# coding:utf-8
stations = { 'ZhenJiang': 'ZJH', #鎮江
'ZhenJiangNan': 'ZEH', #鎮江南
'ShangHai': 'SHH'} #上海
附截圖(程式碼檔名為stations.py):

image
六、編寫查詢餘票的程式
程式碼如下:
# coding:utf-8
"""命令列火車票檢視器
Usage:
tickets [-gdtkz] <from> <to> <date>
"""
from docopt import docopt
from stations import stations
import requests
from prettytable import PrettyTable
from colorama import init, Fore
init()
class TrainsCollection:
header = '車次 車站 時間 經歷時 一等座 二等座 高階軟臥 軟臥 硬臥 硬座 無座'.split()
def __init__(self, available_trains,available_place, options):
"""查詢的火車班次集合
:param available_trains: 一個列表, 包含可獲得的火車班次, 每個
火車班次是一個字典
:param options: 查詢的選項, 如高鐵, 動車, etc...
"""
self.available_trains = available_trains
self.available_place = available_place
self.options = options
@property
def trains(self):
for raw_train in self.available_trains:
raw_train_list = raw_train.split('|')
train_no = raw_train_list[3]
initial = train_no[0].lower()
duration = raw_train_list[10]
if initial in self.options:
train = [
train_no,
'\n'.join([Fore.LIGHTGREEN_EX + self.available_place[raw_train_list[6]] + Fore.RESET,
Fore.LIGHTRED_EX + self.available_place[raw_train_list[7]] + Fore.RESET]),
'\n'.join([Fore.LIGHTGREEN_EX + raw_train_list[8] + Fore.RESET,
Fore.LIGHTRED_EX + raw_train_list[9] + Fore.RESET]),
duration,
raw_train_list[-6] if raw_train_list[-6] else '--',
raw_train_list[-7] if raw_train_list[-7] else '--',
raw_train_list[-12] if raw_train_list[-12] else '--',
raw_train_list[-14] if raw_train_list[-14] else '--',
raw_train_list[-9] if raw_train_list[-9] else '--',
raw_train_list[-8] if raw_train_list[-8] else '--',
raw_train_list[-11] if raw_train_list[-11] else '--',
]
yield train
def pretty_print(self):
pt = PrettyTable()
pt._set_field_names(self.header)
for train in self.trains:
pt.add_row(train)
print(pt)
def cli():
"""command-line interface"""
arguments = docopt(__doc__)
from_station = stations.get(arguments['<from>'])
to_station = stations.get(arguments['<to>'])
date = arguments['<date>']
url = 'https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date={}&leftTicketDTO.from_station={}&leftTicketDTO.to_station={}&purpose_codes=ADULT'.format(
date, from_station, to_station)
r = requests.get(url, verify=False)
available_trains = r.json()['data']['result']
available_place = r.json()['data']['map']
options = ''.join([
key for key, value in arguments.items() if value is True
])
TrainsCollection(available_trains,available_place, options).pretty_print()
if __name__ == '__main__':
cli()
附截圖(程式碼檔名為tickets.py):

image
七、設定出發站、目的站等引數
Eclipse裡面設定相關的引數,引數列表內容如下:
-gd ShangHai ZhenJiang 2018-01-28
附截圖:

image
八、執行查詢餘票的指令碼
在eclipse裡執行該指令碼
附截圖:

image
相關文章
- 是程式設計師就用Python查12306的票程式設計師Python
- Python學習筆記之12306搶票Python筆記
- Python3.6實現12306火車票自動搶票Python
- 12306購票送溫暖
- 最新12306搶票爬蟲爬蟲
- 公積金餘額查詢電話
- 12306候補購票怎麼用?12306火車票候補購票使用攻略和注意事項
- 12306火車票搶票Python程式碼最新完整版釋出,五一搶票就靠它了!Python
- 使用Python編寫一個多執行緒的12306搶票程式Python執行緒
- python天氣查詢Python
- PostgreSQL與12306搶火車票的思考SQL
- 教你用Python動重新整理搶12306火車票,附原始碼!Python原始碼
- 12306搶票系統無介面版本——(1)登入(12306驗證碼問題破解)
- oracle 剩餘表空間查詢慢,解決辦法Oracle
- Python小技巧 - 子串查詢Python
- Python模組查詢路徑Python
- python 當中的模糊查詢Python
- python實現查詢糾錯Python
- python二分查詢模板Python
- 從零實現一款12306搶票軟體
- 怎麼查詢python裝在哪裡Python
- python檔案查詢之find命令Python
- web3.js 互動 geth 實現轉賬餘額查詢功能WebJS
- SQL查詢的:子查詢和多表查詢SQL
- 12306 出招搶票軟體,技術黃牛生意要“黃”?
- 父子元件通訊——模擬12306購票新增乘車人元件
- 高併發!一個仿 12306 鐵路購票專案!
- mysql-分組查詢-子查詢-連線查詢-組合查詢MySql
- Python—Django:關於在Django框架中對資料庫的查詢函式,查詢集和關聯查詢PythonDjango框架資料庫函式
- 新 餘 哪 裡 有 開 發 票
- Python字串string的查詢和替換Python字串
- Algorithm-search 查詢演算法 pythonGo演算法Python
- Python中使用MySQL模糊查詢的方法PythonMySql
- python for迴圈遍歷位置的查詢Python
- python查詢mysql中文亂碼問題PythonMySql
- [Python實戰]Python製作天氣查詢軟體Python
- 複雜查詢—子查詢
- 查詢——二分查詢
- 用PyQt5編輯 12306車票資訊爬取程式QT