python12306买票_自己用python随意写的12306订票代码

#!/usr/bin/env python

# -*- encoding:utf-8 -*-

'''

Created on 2014年12月18日

@author: liujichao

'''

import datetime

import json

import re

import sys

import time

import Image

import PyV8

import requests

import tools.email_helper as emailHelper

reload(sys)

sys.setdefaultencoding('utf-8')# @UndefinedVariable

reqSingle= requests.Session()

attCheCi=["G655","G6741","G67","G491"]#关注的车次

dateList=["2015-02-18"]#关注的日期

username="12306登录用户名"

password="登录密码"

#这个是需要手动提交订单后f12自己找的,挨个post请求去找,参数名为:oldPassengerStr 格式如下

oldPassengerStr="姓名,1,130434199802036011,1_姓名2,1,130434199204238069,1_"

#这个是需要手动提交订单后f12自己找的,挨个post请求去找,参数名为:passengerTicketStr 格式如下

passengerTicketStr="O,0,1,姓名,1,130434199802036011,13683456789,N_O,0,1,姓名2,1,130434199204238069,13683456789,N"

header={

"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",

"Accept-Encoding":"gzip, deflate",

"Accept-Language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",

"Connection":"keep-alive",

"Host":"kyfw.12306.cn",

"Referer":"https://kyfw.12306.cn/otn/safeguard/init",

"User-Agent":"Mozilla/5.0 (Windows NT 5.1; rv:34.0) Gecko/20100101 Firefox/34.0"

}

##定火车票

def orderTicket(fromStation,toStation,trainDate,secretStr):

header["Referer"]="https://kyfw.12306.cn/otn/leftTicket/init"

orderInitReq= reqSingle.get("https://kyfw.12306.cn/otn/leftTicket/init",headers=header)

header["Referer"]="https://kyfw.12306.cn/otn/leftTicket/init"

aryKV=extractKey(orderInitReq.content,header)

print aryKV

#初始化订票

header["Referer"]="https://kyfw.12306.cn/otn/leftTicket/init"

orderInitReq= reqSingle.post("https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest",data={

aryKV[0]:aryKV[1],

"train_date":trainDate,

"myversion":"undefined",

"purpose_codes":"ADULT",

"query_from_station_name":fromStation,

"query_to_station_name":toStation,

"secretStr":secretStr,

"tour_flag":"dc",

"back_train_date":time.strftime('%Y-%m-%d',time.localtime(time.time())),

"undefined":""

},headers=header)

print orderInitReq.content

orderInitJson=orderInitReq.json()

if orderInitJson.get("status")==False or  orderInitJson.get("httpstatus")!=200:

raise Exception("订票出现错误")

initDcReq= reqSingle.post("https://kyfw.12306.cn/otn/confirmPassenger/initDc", data={"_json_att":""},headers=header)

header["Ref

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值