1.首先從如何查票開始做
qeury_ticket.py獲取車次在程式中的簡寫
station.py 查詢車票
station.py
# -*- coding: utf-8 -*-
'''該檔案主要是獲取車站的資訊
'''import re,requests
from requests.packages.urllib3.exceptions import insecurerequestwarning
requests.packages.urllib3.disable_warnings(insecurerequestwarning)
url = r""
response = requests.get(url,verify=
false
)station_name=
(response.content.split(
'='))[
1]list_name= station_name.split(
'@')
station_list=
for x in list_name:if(
len(x)
>1)
:for x in station_list:
print x
pass
qeury_ticket.py
```python
# -*- coding: utf-8 -*-
import re,requests
from requests.packages.urllib3.exceptions import insecurerequestwarning
import json
requests.packages.urllib3.disable_warnings(insecurerequestwarning)
defcheci_
(sd)
: checi_info=
return checi_info[sd]
defmain()
:# date = input("請輸入時間(如2019-01-22):\n")
# from_station = chezhan_code[input("請輸入起始站點:\n")]
# to_station = chezhan_code[input("請輸入目的站點:\n")]
url =
""headers =
url=url+
"leftticketdto.train_date="
+r"2019-11-24"
+"&leftticketdto.from_station="
+"xay"
+"&leftticketdto.to_station="
+r"hzh"
+"&purpose_codes=adult"
#print(url) 已經檢查過生成的url是正確的
#request請求獲取主頁
r = requests.get(url,headers=headers,verify=
false
) r.raise_for_status(
)#如果傳送了乙個錯誤的請求,會丟擲異常
result=((
((r.content)
.split(
'['))[
1]).split(
']'))[
0]#處理返回資料
re_list= result.split(
",")
for x in re_list:
ticket_info=x.split(
'|')
checi=ticket_info[3]
station_start=checi_(ticket_info[4]
) station_stop=checi_(ticket_info[7]
) start_time=ticket_info[8]
stop_time=ticket_info[9]
time_****ed_up =ticket_info[10]
if(len(ticket_info[31]
)==0)
: first_class_seat=
"無"else
: first_class_seat=ticket_info[31]
if(len(ticket_info[30]
)==0)
: second_class_seat=
"無"else
: second_class_seat=ticket_info[30]
if(len(ticket_info[23]
)==0)
: soft_sleep=
"無"else
: soft_sleep=ticket_info[23]
if(len(ticket_info[28]
)==0)
: hard_sleep=
"無"else
: hard_sleep=ticket_info[28]
if(len(ticket_info[29]
)==0)
: hard_seat=
"無"else
: hard_seat=ticket_info[29]
if(len(ticket_info[26]
)==0)
: no_seat=
"無"else
: no_seat=ticket_info[26]
print
'出發站:'
+station_start+
'\n'
+"終點站:"
+station_stop+
'\n'
++start_time+
'\n'
++stop_time+
'\n'
+"耗時:"
+time_****ed_up+
'\n'
+"一等座:"
+first_class_seat+
'\n'
+"二等座:"
+second_class_seat+
'\n'
+"軟臥:"
+soft_sleep+
'\n'
+"硬臥:"
+hard_sleep+
'\n'
+"硬座:"
+hard_seat+
'\n'
+"無座:"
+no_seat+
'\n'
+'\n\n'
main(
)
Python操作12306搶票指令碼
首先,我需要的工具和元件有 基本的配置已完成,測試一下,cmd下執行python,分別鍵入以下指令 這個時候大家可能會遇到一些 情況 chromedriver.exe程序 會經常關閉 解決辦法 chrome 版本第一點 52.xx.xx就可以 然後 去找對應版本的 chromedriver 還有遇到...
12306搶票軟體
import json,time 讀寫資料庫中的票數 defwr info sign,dic none if sign r with open ticket mode r encoding utf 8 as fp dic json.load fp return dic elif sign w wit...
12306搶票一(登入)
找到登入的頁面,由於賬號密碼登入比較麻煩就直接掃碼登入所以要先找到登入頁面的url,利用selenium進行登入。在實戰中一般要先把框架給搭好,好後面新增 為了 得簡潔,可讀性較高,所以整個過程在乙個類中實現,類中又定義了不同的函式來實現不同的功能 搭框架 有乙個執行的入口,有乙個主函式可以呼叫定義...