`

....

阅读更多
#!/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')
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics