[Python] 纯文本查看 复制代码
#详情见源码,分享给苏州大众,其他地区参考:
#更新:20240311
#100-107行更新公交实时查询中空数据情况bug
#今日苏州公交实时查询202303092347
#增加首末时间
#增加行车间隔
#增加公交站对应的地铁🚇换车信息
import requests,re,json,time
from pprint import pprint
from prettytable import PrettyTable
def get_line(word):
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'}
url='https://szgj.2500.tv/line/search?keyword={}'.format(word)
res=requests.get(url,headers=headers)
#print(res.headers)
#print(res.content.decode())
name=re.findall('<div class="buslinename">(.*?)</div>',res.text,re.S)[0]
#href=re.findall('<a href="(.*?)">.*?<div class="buslineinfo historydiv">',res.text,re.S)[0]
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(name,linetosl,route)
#print(code)
print(1,linetosl[0])
print(2,linetosl[1])
select=input('pls select bus route:')
if select=='1':return name,linetosl[0],route[0]
else:return name,linetosl[1],route[1]
def bus_luxian(name,linetosl,route):
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'}
code=route.split('=')[1]
res=requests.get(route,headers=headers)
#print(res.status_code)
#print(res.content.decode())
sm=re.findall('<span class="bigicon.*?">(.*?)</span><span>.*?</span>',res.text,re.S)#首
smt=re.findall('<span class="bigicon.*?">.*?</span><span>(.*?)</span>',res.text,re.S)#末
xcjg=re.findall('var msg = "(.*?)"',res.text,re.S)[0]#行车间隔
nums=re.findall('div class="stationnum.*?">(.*?)</div>',res.text,re.S)#数
ids=re.findall('<div class="stationdetail" data-sid="(.*?)">',res.text,re.S)#id
snames=re.findall('<div class="stationname".*?>(.*?)</div>',res.text,re.S)#name
subway=re.findall('subway\?line=(.*?)&',res.text,re.S)
subway_id=re.findall('&sguid=(.*?)&',res.text,re.S)
#print(nums,ids,subway,subway_id,len(subway),len(subway_id))
sinfos=[]
for i in range(len(ids)):sinfos.append('')
for i in range(len(subway_id)):
if subway_id[i] in ids:#增加判断
sarea=ids.index(subway_id[i])
if sinfos[sarea]!='':sinfos[sarea]+='_'+subway[i]
else:sinfos[sarea]='_'+subway[i]
#print(len(sinfos),len(ids))
endname=re.findall('<div class="stationname">(.*?)</div>',res.text,re.S)
#print(endname)
if endname!=[]:
endname=endname[0]
for i in range(len(snames)):
if snames[i]=='':
snames[i]=endname
#获取运行中的公交车和时间
url='https://szgj.2500.tv/api/v1/busline/bus?line_guid={}'
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-With':'XMLHttpRequest',
'Referer': route,
'Host': 'szgj.2500.tv'}
res=requests.get(url.format(code),headers=headers)
js=json.loads(res.content.decode())
pprint(js)
print('\n公交:',name)
print(sm[0]+':'+smt[0],sm[1]+':'+smt[1])
if xcjg!='':print(xcjg)
print('--------------------------')
print('线路:',linetosl)
if js.get('data')!=None:
print('下一班:',js['data']['nextBus'],js['data']['nextShift'],'\n')
#if js['data']['number']!=0:
if len(js['data']['standInfo'])!=0:
info=js['data']['standInfo']
#print(info)
items=list(info.items())
#print(items)
infos=[]
for i in range(len(ids)):infos.append(' '*19)
for key,value in items:
if value!=[]:#添加空数据异常
if key in ids:#增加判断
area=ids.index(key)
infos[area]=value[0]['inTime']+' '+value[0]['busInfo']
tb = PrettyTable()
tb.align = 'c'
tb.add_column('id',nums)
tb.add_column('info',infos)
tb.add_column('station',snames)
for i in sinfos:
if i!='':
tb.add_column('subway',sinfos)
break
#print(tb)
tb.align = 'l'
print('\033[1;37m{}\033[0m'.format(tb))
else:print('无正在运行公交,或数据异常')
def bus_run():
#while True:
print(time.asctime(time.localtime(time.time())))
word=input('-> Enter bus num: ')
if word=='3':
word='快线3号'
line=get_line(word)
luxian=bus_luxian(line[0],line[1],line[2])
if __name__=='__main__':
bus_run()