码迷,mamicode.com
首页 > Web开发 > 详细

webpy 实例 12306余票查询

时间:2015-01-26 20:58:57      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

效果

技术分享

main.py

# -*- coding: utf-8 -*-
import web
import search

urls = (
    ‘/‘, ‘Search‘,
)

render = web.template.render(‘templates‘)

class Search():
    def GET(self):
        return render.search([])
    
    def POST(self):
        x = web.input()
        data = search.getInfoByUrl(str(x[‘date‘]),str(x[‘from‘]),str(x[‘to‘]))
        return render.search(data)
        
app = web.application(urls, globals())

if __name__=="__main__":
    app.run()

 search.py

# -*- coding: utf-8 -*-
import urllib
import re
import json

SEARCH_TICKET_URL="https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate=%s&from_station=%s&to_station=%s"

def getStationName(chinese):
    stationTable = open("station_name.js").read()
    pattern = re.compile(‘\|‘+chinese+‘\|([A-Z]{3})‘);
    stationCode = pattern.search(stationTable)
    return stationCode.group(1)
    

def getInfoByUrl(queryDate, fromStation, toStation):
    fromStation = getStationName(fromStation)
    toStation = getStationName(toStation)
    url = SEARCH_TICKET_URL % (queryDate, fromStation, toStation)
    print url
    rawJson = urllib.urlopen(url).read()
    data = json.loads(rawJson)
    return data["data"]["datas"]

#queryDate = ‘2015-01-27‘
#fromStation = ‘成都‘
#toStation = ‘北京‘
#print getInfoByUrl(queryDate,fromStation,toStation)

 template/search.html

$def with(trains)
<html>
<head>
<style>
table {
  font-size: 12px;
  text-align: center;
}
.th {
  background-color: #288CCC;
  color: #FFF;
}
</style>
</head>

<body>
<form method="post">
日期 <input type="text" name="date" value="2015-01-27">
出发地 <input type="text" name="from" value="北京">
目的地 <input type="text" name="to" value="成都">
<input type="submit" value="提交">
</form>

<table>
<thead>
<tr class="th">
  <th width="90">车次</th>
  <th width="100"><div class="two-line"><span>出发站</span><br clear="none" /><span>到达站</span></div></th>
  <th width="82"><div class="two-line" id="startendtime"><span>出发时间</span><br clear="none" /><span>到达时间</span></div></th>
  <th width="82"><span class="b2" id="_span_lishi">历时</span></th>
  <th width="49">商务座</th>
  <th width="49">特等座</th>
  <th width="49">一等座</th>
  <th width="49">二等座</th>
  <th width="49">高级<br clear="none" />
  软卧</th>
  <th width="49">软卧</th>
  <th width="49">硬卧</th>
  <th width="49">软座</th>
  <th width="49">硬座</th>
  <th width="49">无座</th>
  <th width="49">其它</th>
  <th class="last">备注</th>
</tr>
</thead>
<tbody id="_query_table_datas"></tbody>

$for train in trains:
  <tr class="" id="$train[‘train_no‘]">
    <td  class="train">
    $train[‘station_train_code‘]
    </td>
    <td class="cdz">
        <strong class="start-t">$train[‘start_station_name‘]</strong><br>
        <strong>$train[‘end_station_name‘]</strong>          
    </td>
    <td class="cds">
        <strong class="start-t">$train[‘start_time‘]</strong><br>
        <strong>$train[‘arrive_time‘]</strong>
    </td>
    <td class="ls">
        <strong>$train[‘lishi‘]</strong>
        <span></span>
    </td>
    <td width="49"><span>$train[‘swz_num‘]</span></td> 
    <td width="49"><span>$train[‘tz_num‘]</span></td>
    <td width="49"><span>$train[‘zy_num‘]</span></td>
    <td width="49"><span>$train[‘ze_num‘]</span></td>
    <td width="49"><span>$train[‘gg_num‘]</span></td>
    <td width="49"><span>$train[‘rw_num‘]</span></td>
    <td width="49"><span>$train[‘yw_num‘]</span></td>
    <td width="49"><span>$train[‘rz_num‘]</span></td>
    <td width="49"><span>$train[‘yz_num‘]</span></td>
    <td width="49"><span>$train[‘wz_num‘]</span></td>
    <td width="49"><span>$train[‘qt_num‘]</span></td>
  </tr>

</table>

</body>
</html>

webpy 实例 12306余票查询

标签:

原文地址:http://www.cnblogs.com/meelo/p/4251206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!