#!/usr/bin/env python
import requests
import urllib.parse
import random
import time
req = requests.session()
import sys
import re
import urllib3
import getpass # 密文輸入
urllib3.disable_warnings()
# 登陸-------------------------
apptklist = []
newStrList = []
def signin():
ran = random.uniform(1, 0)
imgUrl = ('https://kyfw.12306.cn/passport/captcha/captcha-image?'
'login_site=E&module=login&rand=sjrand&%s' % ran)
print(imgUrl)
imgresponse = req.get(url=imgUrl, verify=False)
codeimg = imgresponse.content
fn = open('code.png', 'wb')
fn.write(codeimg)
fn.close()
codeStr = input('請輸入驗證碼的坐標:')
a = ''
b = ''
c = ''
d = ''
e = ''
f = ''
g = ''
h = ''
if '1' in codeStr:
a = '37,37,'
if '2' in codeStr:
b = '100,37,'
if '3' in codeStr:
c = '180,37,'
if '4' in codeStr:
d = '250,37,'
if '5' in codeStr:
e = '37,100,'
if '6' in codeStr:
f = '100,100,'
if '7' in codeStr:
g = '180,100,'
if '8' in codeStr:
h = '250,100,'
newCodeStr = a+b+c+d+e+f+g+h
newStr = newCodeStr[:-1]
newStrList.append(newStr)
url = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
data = {
'answer':newStr,
'login_site':'E',
'rand':'sjrand'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/63.0.3239.108 Safari/537.36',
}
response = req.post(url=url, data=data, headers=headers, verify=False)
print('檢測圖片-----------', url)
print(response.text)
result = response.json()
if result['result_code'] == '4':
print('驗證碼校驗成功')
else:
print('驗證碼校驗失敗,請注意填寫正確的坐標')
signin()
return
userName = input('Please input your userName:')
# password = input('Please input your password:')
password = getpass.getpass('Please input your password:')
loginData = {
'username':userName,
'password':password,
'appid':'otn'
}
headers = {
'Host':'kyfw.12306.cn',
'Referer':'https://kyfw.12306.cn/otn/login/init',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2)'
' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 '
'Safari/537.36'
}
response = req.post('https://kyfw.12306.cn/passport/web/login', data=loginData, headers=headers, verify=False)
print('--------------登陸中--------------------')
print('https://kyfw.12306.cn/passport/web/login')
print('response', response.text.encode('utf-8').decode('utf-8'))
fd = open("locate.html", 'wb+')
fd.write(response.content)
yzData = {
'appid':'otn'
}
response = req.post('https://kyfw.12306.cn/passport/web/auth/uamtk', data=yzData, headers=headers, verify=False)
print('---------------------第一次驗證---------------------')
print(response.text)
# print('typeof response',type(response))
loginMessage = response.json()['newapptk']
print('loginMessage=', loginMessage)
# 第二次驗證開始++++++++++++++++++++++++++++++
yz2Data = {
'tk': loginMessage
}
response = req.post('https://kyfw.12306.cn/otn/uamauthclient', data=yz2Data, headers=headers,verify=False)
print('---------------------第二次驗證---------------------')
print(response.text)
apptk = response.json()['apptk']
apptklist.append(apptk)
def buy():
print(newStrList)
req.headers['Referer'] = 'https://kyfw.12306.cn/otn/leftTicket/init'
result = req.post('https://kyfw.12306.cn/otn/login/checkUser')
print('----------------購票系統(tǒng)--------------')
print(result.text)
print('驗證登錄狀態(tài)成功checkUser')
headers = {
'Referer':'https://kyfw.12306.cn/otn/leftTicket/init',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36'
' (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
}
data = {
# 'secretStr':reserve[1],
'train_date':'2018-01-17',
'back_train_date':'2018-01-17',
'tour_flag':'dc', # dc 單程
'purpose_codes':'ADULT', # adult 成人票
'query_from_station_name':'成都',
'query_to_station_name':'長沙',
'undefined':''
}
def ticket():
# 先登陸、然后查詢車票信息
signin()
url = ('https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2018-02-10&'
'leftTicketDTO.from_station=SZQ&leftTicketDTO.to_station=NFG&purpose_codes=ADULT')
try:
response = requests.get(url, verify=False)
result = response.json()
print(result)
return result['data']['result']
except Exception as e:
return None
if __name__ == "__main__":
# ticket()
# with open('./aaa.xlsx', encoding='utf-8') as f:
#
# print(f.read())
dic = {}
context = [('IDS_ABOUT_OFFICAL_PHONE', 'Slu?beni telefon'),
('IDS_ABOUT_OFFICAL_WEBSITE', 'Slu?beno web-mjesto'),
('IDS_ABOUT_OFFICIAL_PHONE_CALL_NOT_SUPPORT', 'Trenutni ure?aj ne mo?e pozivati.')]
for i in range(len(context)):
print(i)
dic.setdefault(context[i][0],context[i][1])
print(dic)
print(dic.keys())
Python 實現(xiàn)12306登陸
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門顽染,熙熙樓的掌柜王于貴愁眉苦臉地迎上來漾岳,“玉大人,你說我怎么就攤上這事粉寞∧峋#” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵唧垦,是天一觀的道長捅儒。 經(jīng)常有香客問我,道長振亮,這世上最難降的妖魔是什么巧还? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮坊秸,結(jié)果婚禮上麸祷,老公的妹妹穿的比我還像新娘。我一直安慰自己褒搔,他們只是感情好阶牍,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著星瘾,像睡著了一般荸恕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上死相,一...
- 文/蒼蘭香墨 我猛地睜開眼倒彰,長吁一口氣:“原來是場噩夢啊……” “哼审洞!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起待讳,我...
- 正文 年R本政府宣布比勉,位于F島的核電站,受9級特大地震影響瑞躺,放射性物質(zhì)發(fā)生泄漏敷搪。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一幢哨、第九天 我趴在偏房一處隱蔽的房頂上張望赡勘。 院中可真熱鬧,春花似錦捞镰、人聲如沸闸与。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽践樱。三九已至,卻和暖如春凸丸,著一層夾襖步出監(jiān)牢的瞬間拷邢,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 昨天老公不在家唆阿,有種回歸到一個人的興奮莱没,但是,也覺得很想念酷鸦,好像少了什么饰躲,魂不守舍牙咏。曾以為,結(jié)了婚之后嘹裂,就明白了什...
- 30分鐘《歷史》資料妄壶。戰(zhàn)國時期的社會變化,公元前475年至公元前221年寄狼,三晉分家:晉國被韓趙魏瓜分丁寄,田氏代齊:也...