#!/usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "chenzh"
# __version__ = "1.0.7"
# __copyright__ = "Copyright (c) 2010-2010 DreamFlyingFish"
# __license__ = "GNU"
import re
import urlparse
import urllib
class Businfo:
baseurl = 'http://www.zuoche.com/traffic/'
scity = '广州' #城市
dcity = '广州'# 城市
ssite = '华师' #出发地
dsite= '科韵路' #目的地
com = {'x':31, 'y': '8', 'time': '8', 'stgy': '0', 'peop': '0'}#for getchoices
# 非构造器, 但是可以作为初始化函数
def __init__(self):
pass
def getchoices(self, type , scity, dcity, ssite, dsite):
querydict = {'c1': scity , 'c2':dcity , 's':ssite, 'd':dsite}
querydict.update(self.com)
querystr = urllib.urlencode(querydict)
dsturl =self.baseurl + '?' + querystr
res = urllib.urlopen(dsturl)
content = res.read()
res.close()
if type == 'si':
restr = '<select.*name=si[\s\S]*?</select>'
else :
restr = '<select.*name=di[\s\S]*?</select>'
subrestr = '<option[^>]*>[^<]*</option>'
result = re.search(restr, content)
if result is None:
return None
restrrs = result.group()
opts = re.findall(subrestr, restrrs)
ls = []
for opt in opts:
txt = re.sub('<option[^>]*>|</option>', '', opt)
i = re.sub('<option[^>]*value="|".*</option>', '', opt)
ls.append([txt,i] )
return dict(ls)
def func(self):
print 'hello world'
def fetch_bus_info(self, csi, cdi, scity, dcity):
querydict = {'si': ss[ss.keys()[csi]], 'di': ds[ds.keys()[cdi]], 'c1': scity , 'c2':dcity }
querydict.update(self.com)
# 获取公交信息列表
qstr = urllib.urlencode(querydict)
dsturl =self.baseurl + '?' + qstr
response = urllib.urlopen(dsturl)
ct = response.read()
response.close()
#print ct.decode('utf-8')
bus_re = '<td[^>]*id="caption.*?"[^>]*>[\s\S]*?<br[\s\S]*?/>'
bus_info_list = re.findall(bus_re, ct)
return bus_info_list
def showls(ls):
if ls is None:
return None
index = 0
for index, key in enumerate(ls.keys()):
print index,
print key.decode('utf-8'),
print ls.get(key)
#python调用时执行 当被导入时没有执行
if __name__=='__main__':
scity = '广州' #城市
dcity = '广州'# 城市
ssite = '华师' #出发地
dsite= '科韵路' #目的地
businfo = Businfo()
ss = businfo.getchoices('si', scity, dcity, ssite, dsite)
showls(ss)
csi = int(raw_input('you start:'))
ds = businfo.getchoices('di', scity, dcity, ssite, dsite)
showls(ds)
cdi = int(raw_input('you end :'))
print 'your start:%s your end:%s' % (ss[ss.keys()[csi]] ,ds[ds.keys()[cdi]] )
for bus in businfo.fetch_bus_info(csi, cdi, scity, dcity):
print re.sub('<[\s\S]*?>', '', bus).decode('utf-8')
分享到:
相关推荐
■Chapter 12: Fonts................................................................................................................................... 137 ■Chapter 13: Embedding the WebKit Browser......
目录.....................................................................................................................................................2 第一章 JavaScript 快速入门......................
欢迎使用...........................................................................................................................................2 1. 产品简介 ........................................
一级保护要求...............................................................................................................................................56.1 系统运维管理...............................
测量.............
整理了一些PHP的类,希望能对大家有所帮助 activeCodeObj.class.php ascii.class.php book.class.php cache2.class.php database.class.php file_dir.class.php . . . . . .
目录 .........................................................................................................................................................1 JVM ......................................
2--[震惊,你家电脑可能安装了一个假的Windows,速来查看......].zip源码scratch2.0 3.0编程项目源2--[震惊,你家电脑可能安装了一个假的Windows,速来查看......].zip源码scratch2.0 3.0编程项目源2--[震惊,你家...
东菱EPS-B2系列伺服驱动器技术手册pdf,安全注意事项........................................................................................................................................... 2 第一章...
1.4 范式...................................................................................................................................................4 1.4.1 第一范式................................
1 概述 ................................................................................................................................................. 5 1.1 项目背景 ..................................
..............................362.3.4 Traceview性能分析工具 ......................................372.4 Git版本控制系统 ...............................................382.4.1 Git简介 ....................
特效添加..........
Contents Contents ii List of Tables x List of Figures xiv 1 Scope 1 2 Normative references 2 3 Terms and definitions 3 4 General principles 7 4.1 Implementation compliance . ....4.2 Structure of this ...
免费下载,大家欣赏..................
基于Openstack的云桌面管理平台设计...................................................................................234.1 平台架构设计.....................................................................
.............................................................................50二、公共交通票务系统 ...............................................................................52三、航空行李追踪 .....
.................................................................25(一)、当前技术特点......................................................................................................................
WINDOWS和LINUX文件互传............