本节代码为登录成功并打印出账户名称
- #-*-coding:utf-8 -*-
- import urllib2
- import urllib
- # 验证码登录同步
- import cookielib
- import damatuWeb
- import ssl
- from json import loads
- from cons import station_name
- import time
- header={
- "Referer":"https://kyfw.12306.cn/otn/leftTicket/init",
- "User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
- }
- #证书验证
- ssl._create_default_https_context = ssl._create_unverified_context
- # 请求验证码图片
- c=cookielib.LWPCookieJar()#生成一个储存cookie的对象
- cookie=urllib2.HTTPCookieProcessor(c)#绑定
- opener=urllib2.build_opener(cookie)
- urllib2.install_opener(opener)
- # opener绑定,用opener.open请求,自带cookie,实现验证码与登陆同步
- city={}
- for i in station_name.split("@"):
- if not i:
- continue
- tmp_list=i.split("|")
- city[tmp_list[1]]=tmp_list[2]
- # print city["长沙"]
- # print city["上海"]
- from_station=city["长沙"]
- to_station=city["上海"]
- train_date="2017-11-08"
- def getCode():
- req=urllib2.Request("https://kyfw.12306.cn/passport/captcha/"
- "captcha-image?login_site=E&module="
- "login&rand=sjrand&0.3835166812770916")
- codeing=opener.open(req).read()#opener
- fn=open('code/code.png','wb')
- #保存到本地
- fn.write(codeing)
- fn.close()
- def code():
- getCode()
- req=urllib2.Request('https://kyfw.12306.cn/passport'
- '/captcha/captcha-check')
- code=damatuWeb.calcCode()
- # code=raw_input(">>")
- print code
- data={
- "answer":code,
- "login_site":"E",
- "rand":"sjrand",
- }
- data=urllib.urlencode(data)#把字典类型转变为查询字符串
- html=opener.open(req,data=data).read()
- result=loads(html)
- if result["result_code"]=="4":
- print"验证码校验成功!"
- return True
- else:
- print "验证码校验失败"
- code()
- def login():
- #登录
- req=urllib2.Request('https://kyfw.12306.cn/passport'
- '/web/login')
- data={
- 'username':'test',
- 'password':'test',
- 'appid':'otn',
- }
- data = urllib.urlencode(data)
- html = opener.open(req, data=data).read()
- result = loads(html)
- req = urllib2.Request('https://kyfw.12306.cn/passport/web/auth/uamtk')
- req.headers = header
- html = opener.open(req, data='appid=otn').read()
- print html
- result = loads(html)
- newapptk = result['newapptk']
- req = urllib2.Request('https://kyfw.12306.cn/otn/uamauthclient')
- req.headers = header
- html = opener.open(req, data='tk=%s' % newapptk).read()
- print html
- if result['result_code'] == 0:
- print '登录成功!'
- return True
- else:
- print '登录失败'
- def getTrain():
- req=urllib2.Request("https://kyfw.12306.cn/otn/leftTicket/query?"
- "leftTicketDTO.train_date=%s&leftTicketDTO"
- ".from_station=%s&leftTicketDTO.to_station=%s&"
- "purpose_codes=ADULT" %(train_date,from_station,to_station))
- req.headers=header
- html=opener.open(req).read()
- try:
- result = loads(html)
- return result['data']['result']
- except Exception,e:
- time.sleep(2)
- getTrain()
- #下单
- def order(secretStr):
- #第1个请求
- req = urllib2.Request('https://kyfw.12306.cn/otn/login/checkUser')
- html = opener.open(req,data='_json_att=').read()
- # print html
- #第2个请求
- req = urllib2.Request('https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest')
- data = {
- 'secretStr':secretStr,
- 'train_date':'2017-11-08',
- 'back_train_date':'2017-11-05',
- 'tour_flag':'dc',
- 'purpose_codes':'ADULT',
- 'query_from_station_name':'长沙',
- 'query_to_station_name':'上海',
- 'undefined':''
- }
- data = urllib.urlencode(data)
- req.headers = header
- html = opener.open(req,data=data)
- # print html.read()
- # print html.geturl()
- req = urllib2.Request('https://kyfw.12306.cn/otn/index/initMy12306')
- req.headers = header
- html = opener.open(req).read()
- print html
- code()
- login()
- # print getTrain()
- #查票
- for i in getTrain():
- tmp_list=i.split("|")
- # tmp_list[3]="车次"
- # tmp_list[8]="出发时间"
- # tmp_list[9]="到达时间"
- # tmp_list[26]="无座"
- # tmp_list[29]="硬座"
- # tmp_list[25]="特等座"
- # tmp_list[31]="一等座"
- # tmp_list[30]="二等座"
- # tmp_list[23]="软卧"
- secretStr=tmp_list[0]
- # a=0
- # for n in tmp_list:
- # print "[%s]%s"%(a,n)
- # a+=1
- # break
- # print tmp_list
- if tmp_list[30]!=u"无" and tmp_list[30]!="":
- print "有票","车次:",tmp_list[3]
- break
- else:
- print "没票"
- # print secretStr
- order(secretStr)