caliph21 发表于 2022-10-27 17:09

苏州公交查询

import requests,re,json,time

from pprint import pprint

from prettytable import PrettyTable

import prettytable as pt

#print(time.localtime(time.time()))

#print(time.asctime(time.localtime(time.time())))



       

def get_line(word):       

        headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Mobile Safari/537.36'}

       

        url='https://szgj.2500.tv/line/search?keyword={}'.format(word)

        res=requests.get(url,headers=headers)

        #print(res.status_code)

        #print(res.content.decode())

        name=re.findall('<div class="buslinename">(.*?)</div>',res.text,re.S)

        linetosl=re.findall('div class="buslineto sltext">(.*?)</div>',res.text,re.S)

        route=re.findall('<div class="routeline" onclick=.*?<a href="(.*?)">',res.text,re.S)

       

        #print(code)

        print(1,linetosl)

        print(2,linetosl)

        select=input('pls select bus route:')

        if select=='1':return name,linetosl,route

        else:return name,linetosl,route

       

def bus_luxian(name,linetosl,route):

        headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Mobile Safari/537.36'}

        code=route.split('=')

        res=requests.get(route,headers=headers)

        #print(res.status_code)

        #print(res.content.decode())

        nums=re.findall('div class="stationnum.*?">(.*?)</div>',res.text,re.S)

        ids=re.findall('<div class="stationdetail" data-sid="(.*?)"></div>',res.text,re.S)

        #sname=re.findall('<div class="stationname".*?>(.*?)</div>',res.text,re.S)      

        snames=re.findall('onclick="gotoStation.*?">(.*?)</div>',res.text,re.S)

        #print(len(nums),len(ids),len(snames))         

       

        #获取运行中的公交车和时间         

        url='https://szgj.2500.tv/api/v1/busline/bus?line_guid={}'

        res=requests.get(url.format(code),headers=headers)

        #print(res.status_code)

        js=json.loads(res.content.decode())

        #json=json.loads(res.text)

        #pprint(json['data'])

        print('--------------------------')

        print('公交:',name)

        print('线路:',linetosl)

        print('下一班:',js['data']['nextBus'],js['data']['nextShift'],'\n')

       

        info=js['data']['standInfo']

        #print(list(info.keys))

        items=list(info.items())

        infos=[]

       

        for i in range(len(ids)):infos.append(' '*19)

        for key,value in items:

               

                #print(value['busInfo'])

                #print(value['inTime'])

                area=ids.index(key)

                infos=value['inTime']+' '+value['busInfo']

                #print(len(infos))

       

        #print(infos)

        #print(snames)

        tb = PrettyTable()

        tb.align = 'r'

        tb.add_column('id',nums)

        tb.add_column('info',infos)

        tb.add_column('id',snames)

        #print(tb)

        tb.align = 'l'

        print('\033[1;37m{}\033[0m'.format(tb))



def bus_run():

        #while True:

        print(time.asctime(time.localtime(time.time())))               

        word=input('-> Enter bus num: ')

        line=get_line(word)

        luxian=bus_luxian(line,line,line)

       

if __name__=='__main__':

        bus_run()

       

caliph21 发表于 2023-3-9 10:47

今日苏州公交,403浏览器禁止访问了,解决办法:
替换headers
headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 10; SP300 Build/CMDCSP300; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/107.0.5304.141 Mobile Safari/537.36 XWEB/5015 MMWEBSDK/20230202 MMWEBID/2257 MicroMessenger/8.0.33.2320(0x2800213D) WeChat/arm64 Weixin NetType/5G Language/zh_CN ABI/arm64',
        'X-Requested':'com.tencent.mm',
        'Referer': 'https://szgj.2500.tv/',
        'Upgrade-Insecure-Requests': '1'}

xuysss 发表于 2022-10-27 17:12

谢谢分享,赞

stertao 发表于 2022-10-27 17:12

谢谢分享。。学习中。。。:lol

ginobili84 发表于 2022-10-27 17:15

学习中,谢谢分享

panya806 发表于 2022-10-27 17:17

厉害厉害&#128077;&#127995;

wangding0625 发表于 2022-10-27 17:19

感谢分享

whatiwhere 发表于 2022-10-27 17:24

qs 能用

yzx159357123 发表于 2022-10-27 17:41

哈哈哈哈哈哈哈哈哈哈哈哈哈哈感谢分享

4899 发表于 2022-10-27 18:00

这是苏州的兄弟么

假笑 发表于 2022-10-27 18:18

这个可以做课设喔
页: [1] 2 3
查看完整版本: 苏州公交查询