[Python] 纯文本查看 复制代码
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)[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(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 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Mobile Safari/537.36'}
code=route.split('=')[1]
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)[0])
items=list(info.items())
infos=[]
for i in range(len(ids)):infos.append(' '*19)
for key,value in items:
#print(value[0]['busInfo'])
#print(value[0]['inTime'])
area=ids.index(key)
infos[area]=value[0]['inTime']+' '+value[0]['busInfo']
#print(len(infos[area]))
#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[0],line[1],line[2])
if __name__=='__main__':
bus_run()