吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2453|回复: 29
收起左侧

[Python 原创] 今日苏州公交实时查询+地铁换乘+首末时间+行车间隔

  [复制链接]
caliph21 发表于 2023-3-10 08:18
本帖最后由 caliph21 于 2024-3-12 16:41 编辑

[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()
	

免费评分

参与人数 7吾爱币 +12 热心值 +7 收起 理由
lingyiling + 1 + 1 我很赞同!
jwdwm01 + 1 + 1 我很赞同!
fjwmir + 1 + 1 用心讨论,共获提升!
caihuang + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
LuckyClover + 1 + 1 我很赞同!
ysy2001 + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

xjbnetboy2022 发表于 2023-12-20 14:17
可能执行
C:\Users\admin>pip install prettytable
Collecting prettytable
  Downloading prettytable-3.9.0-py3-none-any.whl.metadata (26 kB)
Collecting wcwidth (from prettytable)
  Downloading wcwidth-0.2.12-py2.py3-none-any.whl.metadata (14 kB)
Downloading prettytable-3.9.0-py3-none-any.whl (27 kB)
Downloading wcwidth-0.2.12-py2.py3-none-any.whl (34 kB)
Installing collected packages: wcwidth, prettytable
Successfully installed prettytable-3.9.0 wcwidth-0.2.12
kefu800999 发表于 2023-3-10 08:26
qqdns 发表于 2023-3-10 08:27
傲之魂 发表于 2023-3-10 08:28
请问这是调用了哪家的API?
babyz301 发表于 2023-3-10 08:40

感谢分享,学习了
 楼主| caliph21 发表于 2023-3-10 08:43
公众号:今日苏州公交
Js_Aaron 发表于 2023-3-10 08:45
你竟然有接口!太猛了吧,花錢不
aa2923821a 发表于 2023-3-10 08:45
我去  楼主咋得到的
LuckyClover 发表于 2023-3-10 09:06
牛批PLUS
艹123 发表于 2023-3-10 09:16
很多替代品
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-21 05:40

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表