Python學習筆記之12306搶票
本筆記基於Python3.6
主要涉及到爬蟲cookie的處理,特別使用,對今後的爬蟲意義深重
主要實現了:
1. 12306登入驗證碼驗證
2,12306賬號的登入
3, 12306餘票查詢
直接貼上原始碼:
# !/usr/bin/env python
# _*_ coding:utf-8 _*_
import ssl#z證照
import requests
# import cookielib
import json
import urllib
from http import cookiejar
from urllib import request, parse
# ssl._creat_default_https_context=ssl._create_unverified_context#不驗證證照
requested=requests.Session()
#獲取驗證碼
def getcode():
url="https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand&0.22323172816879921"
headers={
'Accept':'image/webp,image/*,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate, sdch, br',
'Accept-Language':'zh-CN,zh;q=0.8',
# 'Connection':'keep-alive',
# 'Host':'kyfw.12306.cn',
# 'Referer':'https://kyfw.12306.cn/otn/login/init',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2372.400 QQBrowser/9.5.10801.400'
}
r=requested.get(url=url,headers=headers,verify=False)#將verify置為false是關閉證照驗證,防止證照過期
with open("code.png","wb") as fn:
fn.write(r.content)
# print(r.cookies)
return r.cookies
#驗證驗證碼
def checkcode():
url = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
Axis = {"1": "41,50", "2": "108,50", "3": "182,50", "4": "254,50", "5": "43,120", "6": "111,120",
"7": "175,120",
"8": "250,120"}
png = input("請輸入驗證碼:")
Arr = png.split(',')
code = ''
for val in Arr:
code = code + ',' + Axis[str(val)]
code = code[1:]
url="https://kyfw.12306.cn/passport/captcha/captcha-check"
headers={
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language':'zh-CN,zh;q=0.8',
'Connection':'keep-alive',
'Content-Length': '63',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Host': 'kyfw.12306.cn',
# 'Cookie': cookie,
'Origin': 'https://kyfw.12306.cn',
'Referer': 'https://kyfw.12306.cn/otn/login/init',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2372.400 QQBrowser/9.5.10801.400',
'X-Requested-With': 'XMLHttpRequest'
}
date={
'answer':code,
'login_site':'E',
'rand':'sjrand'
}
r=requested.post(url=url,data=date,headers=headers,verify=False)
# print(r.text)
result=json.loads(r.text)
result=result["result_code"]
return (result)
#登入賬戶
def login():
url="https://kyfw.12306.cn/passport/web/login"
headers={
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9 ',
'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.4793.400 QQBrowser/10.0.745.400',
'X-Requested-With':'XMLHttpRequest'
}
# data="username=491577803%40qq.com&password=zhangli520&appid=otn"
data={
'username': "491577803@qq.com",
"password":"*******",
'appid': 'otn'
}
r=requested.post(url=url,headers=headers,data=data,verify=False)
# print(r.text)
logstat=json.loads(r.text)
logstat=logstat["result_code"]
return logstat
#查詢餘票
def prebuy():
# 請求url:https://kyfw.12306.cn/otn/passport?redirect=/otn/login/userLogin
url = "https://kyfw.12306.cn/otn/passport?redirect=/otn/login/userLogin"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"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.4793.400 QQBrowser/10.0.745.400"
}
r = requested.get(url=url, headers=headers)
# 請求url:https://kyfw.12306.cn/passport/web/auth/uamtk
url = "https://kyfw.12306.cn/passport/web/auth/uamtk"
headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"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.4793.400 QQBrowser/10.0.745.400",
"X-Requested-With": "XMLHttpRequest"
}
data = {"appid": "otn"}
r = requested.post(url=url, data=data, headers=headers, verify=False)
checkresl = json.loads(r.text)
apptk = checkresl["newapptk"]
checkresl = checkresl['result_code']
if checkresl == 0:
print("驗證1通過!!!")
pass
else:
print("驗證1失敗!!!")
# exit(0)
# 請求 url:https://kyfw.12306.cn/otn/uamauthclient
url = "https://kyfw.12306.cn/otn/uamauthclient"
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"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.4793.400 QQBrowser/10.0.745.400",
"X-Requested-With": "XMLHttpRequest"
}
data = {"tk": apptk}
r = requested.post(url=url, data=data, headers=headers, verify=False)
checkres2 = json.loads(r.text)
checkres2 = checkres2["result_code"]
if checkres2 == 0:
print("驗證2通過!!!")
pass
else:
print("驗證2失敗!!!")
# exit(0)
#請求網站url:https://kyfw.12306.cn/otn/leftTicket/init
url="https://kyfw.12306.cn/otn/leftTicket/init"
headers={
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'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.4793.400 QQBrowser/10.0.745.400'
}
r=requested.get(url=url,headers=headers,verify=False)
# print(r.text)
#請求網站https://kyfw.12306.cn/otn/HttpZF/GetJS
url="https://kyfw.12306.cn/otn/HttpZF/GetJS"
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.3176.400 QQBrowser/9.6.11576.400",
}
r=requested.get(url=url,headers=headers,verify=False)
#請求網站https://kyfw.12306.cn/otn/dynamicJs/qrtqmnp
url="https://kyfw.12306.cn/otn/dynamicJs/qrtqmnp"
headers={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.3176.400 QQBrowser/9.6.11576.400"}
r=requested.get(url=url,headers=headers,verify=False)
#請求網站https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=passenger&rand=randp&0.9482634139575532
url="https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=passenger&rand=randp&0.9482634139575532"
headers={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.3176.400 QQBrowser/9.6.11576.400"}
r=requested.get(url=url,headers=headers,verify=False)
#請求網站查詢餘票 url="https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2018-03-07&leftTicketDTO.from_station=CDW&leftTicketDTO.to_station=NJH&purpose_codes=ADULT"
url="https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2018-03-07&leftTicketDTO.from_station=NAF&leftTicketDTO.to_station=CCT&purpose_codes=ADULT"
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.4793.400 QQBrowser/10.0.745.400"
}
r=requested.get(url=url,headers=headers,verify=False)
result=json.loads(r.text)
result=result["data"]
result=result["result"]
print(result)
c=0
for i in result:
temp_list=i.split("|")
for j in temp_list:
print("%s %s"%(j,c))
c=c+1
# print(temp_list)
print("```````````````````````````````````````")
cde0=temp_list[0]
print(cde0)
print("```````````````````````````````````````")
if temp_list[26]=="有":
print("有票")
flag=1
elif temp_list[26] == "無" or temp_list[26]=="":
print("沒票")
flag=0
elif int(temp_list[26])>0:
print("有票")
flag=1
else:
print("沒票")
flag=0
if flag==0:
exit(0)
# 預定 驗證登入狀態https://kyfw.12306.cn/otn/login/checkUser
url="https://kyfw.12306.cn/otn/login/checkUser"
headers={
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"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.4793.400 QQBrowser/10.0.745.400",
"X-Requested-With":"XMLHttpRequest"
}
data={"_json_att":""}
r=requested.post(url=url,headers=headers,verify=False)
print("驗證登入狀態")
print(r.text)
print(r.cookies)
#請求url https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest
url="https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest"
headers={
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.8",
"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.4793.400 QQBrowser/10.0.745.400",
"X-Requested-With": "XMLHttpRequest"
}
cde1="&train_date=2018-03-07&back_train_date=2018-02-27&tour_flag=dc&purpose_codes=ADULT&query_from_station_name=南召&query_to_station_name=長春&undefined"
data={
"secretStr":str(cde0+cde1)
}
print(data)
r=requested.post(url=url,headers=headers,data= data ,verify=False)
print(r.text)
#執行
def run():
cookie=getcode()
result=checkcode()
if result !="4":
print("驗證碼校驗失敗!!!")
run()
else:
print("驗證碼校驗成功!!!")
pass
logstat=login()
if logstat==0 :
print("登入成功!!!")
pass
else:
print("登入失敗!!!")
run()
prebuy()
run()
相關文章
- Python3.6實現12306火車票自動搶票Python
- 最新12306搶票爬蟲爬蟲
- 12306火車票搶票Python程式碼最新完整版釋出,五一搶票就靠它了!Python
- JAVA學習筆記之 多執行緒賣票Java筆記執行緒
- Python學習筆記之序列Python筆記
- 分享12306搶票心得-最終篇
- Python學習筆記|Python之程式Python筆記
- PostgreSQL與12306搶火車票的思考SQL
- Python學習筆記|Python之yield理解Python筆記
- Python學習筆記|Python之索引迭代Python筆記索引
- Python學習筆記|Python之特殊方法Python筆記
- Python學習筆記|Python之檔案操作Python筆記
- Python學習筆記|Python之物件導向Python筆記物件
- Python學習筆記|Python之特殊檔案Python筆記
- Python學習筆記|Python之推導式Python筆記
- Python學習筆記|Python之執行緒Python筆記執行緒
- 使用Python編寫一個多執行緒的12306搶票程式Python執行緒
- 【學習筆記】python筆記Python
- Python學習筆記Python筆記
- Python 3 學習筆記之——物件導向Python筆記物件
- Python學習筆記|Python之pycache資料夾Python筆記
- Python學習筆記|Python之正規表示式Python筆記
- Python學習筆記(隨筆)Python筆記
- 教你用Python動重新整理搶12306火車票,附原始碼!Python原始碼
- 12306 出招搶票軟體,技術黃牛生意要“黃”?
- 從零實現一款12306搶票軟體
- 多家搶票軟體回應12306資料洩漏
- Python查詢12306餘票Python
- Python 3 學習筆記之——基礎語法Python筆記
- Python 3 學習筆記之——資料型別Python筆記資料型別
- Python 3 學習筆記之類與例項Python筆記
- python學習筆記4Python筆記
- python學習筆記(二)Python筆記
- Python學習筆記 - aiohttpPython筆記AIHTTP
- Python 學習筆記(一)Python筆記
- Python學習筆記 - asyncioPython筆記
- Python學習筆記 - queuePython筆記
- Python學習筆記(2)Python筆記