批量车辆违章查询

文章正文
发布时间:2025-07-12 14:31

本帖最后由 hellolouis 于 2022-4-13 11:01 编辑

由于需要管理公司车辆,每月要查询车辆是否有违章,百度上找了一遍也没找到可以批量查询的,于是自己动手写了个小程序。


使用前必须先打开car.xlsx,写入车辆的车牌,发动机号,车架号。否则出现打开闪退的情况。
使用前必须先打开car.xlsx,写入车辆的车牌,发动机号,车架号。否则出现打开闪退的情况。
使用前必须先打开car.xlsx,写入车辆的车牌,发动机号,车架号。否则出现打开闪退的情况。

首次使用需下载 xlrd1.2.0,复制以下代码到命令行

[Python] 纯文本查看 复制代码

pip install xlrd==1.2.0



-------------------------------------------

使用说明

QQ截图20210401154415.png (58.13 KB, 下载次数: 3)

下载附件

2021-4-1 15:49 上传




excel文件里,有两个工作表,分别是车牌号,发动机号(后6位,数字前加 '),车架号(后6位,数字前加 '),保存,运行py文件即可。


截图

123.png (28.49 KB, 下载次数: 1)

下载附件

2022-4-13 10:56 上传



源码

[Python] 纯文本查看 复制代码

import urllib.request import urllib.parse import xlrd import json class Check(): def choose(self): while True: cartype = input('='*25+'\n1 -- 查询小型轿车违章\n2 -- 查询大型汽车违章'+'\n3 -- 同时查询所有类型\n'+'='*25+'\n请输入数字按Enter键确认:\n\n') if cartype == '1': return '02' elif cartype == '2': return '01' elif cartype == '3': return '03' else: print('\n'+'┌'+'─'*11+'┐'+'\n│输入有误,请重新输入。│\n'+'└'+'─'*11+'┘\n') def check(self, hphm, engine, body, hpzl): self.hphm = hphm self.engine = engine self.body = body self.hpzl = hpzl url = 'https://sp0.baidu.com/5LMDcjW6BwF3otqbppnN2DJv/traffic.pae.baidu.com/data/query?city=dongguan&hphm=&hpzl=&engine=&body=&source=pc' data = { 'hphm': self.hphm, 'hpzl': self.hpzl, 'engine': self.engine, 'body': self.body, 'source': 'pc',} data = urllib.parse.urlencode(data).encode('utf-8') response = urllib.request.urlopen(url,data) msgjson = json.loads(response.read()) if 'success' in msgjson['msg'] : if msgjson['data']['count'] == 0: print(hphm+'\t没有违章') else: print(hphm+'\t违章', msgjson['data']['count'],'次') while msgjson['data']['count'] > 0: print('时间: ', msgjson['data']['lists'][msgjson['data']['count']-1]['time']+'\n' '罚款: ', str(msgjson['data']['lists'][msgjson['data']['count']-1]['fine'])+'\n' '扣分: ', str(msgjson['data']['lists'][msgjson['data']['count']-1]['point'])+'\n' '处理: ', str(msgjson['data']['lists'][msgjson['data']['count']-1]['handled'])+'\n' '类型: ', msgjson['data']['lists'][msgjson['data']['count']-1]['violation_type']+'\n' '地址: ', msgjson['data']['lists'][msgjson['data']['count']-1]['address']+'\n') with open(r'查询结果.txt','a+') as file: file.write(f'{hphm}\t违章\n{"时间: ", msgjson["data"]["lists"][msgjson["data"]["count"]-1]["time"]}\n{"罚款: ", msgjson["data"]["lists"][msgjson["data"]["count"]-1]["fine"]}\n{"扣分: ", msgjson["data"]["lists"][msgjson["data"]["count"]-1]["point"]}\n{"处理: ", msgjson["data"]["lists"][msgjson["data"]["count"]-1]["handled"]}\n{"类型: ", msgjson["data"]["lists"][msgjson["data"]["count"]-1]["violation_type"]}\n{"地址: ", msgjson["data"]["lists"][msgjson["data"]["count"]-1]["address"]}\n') msgjson['data']['count'] -= 1 elif '输入参数不合法' in msgjson['msg']: print(hphm, msgjson['msg']) with open(r'查询结果.txt','a+') as file: file.write(f'{hphm}\t查询失败,请检查车辆信息!\n'+'-'*70+'\n') else: print(hphm+'\t查询失败。') def run(self): hpzl = self.choose() if hpzl == '02': print('开始查询,请稍等...\n\n'+'-'*30) for i in range(1, sheet.nrows): self.check(sheet.cell_value(i,0), sheet.cell_value(i,1), sheet.cell_value(i,2), hpzl) print('-'*30) input('\n\n查询结束,按Enter键结束。') elif hpzl == '01': print('开始查询,请稍等...\n\n'+'-'*30) for i in range(1, sheet2.nrows): self.check(sheet2.cell_value(i,0), sheet2.cell_value(i,1), sheet2.cell_value(i,2), hpzl) print('-'*30) input('\n\n查询结束,按Enter键结束。') else: print('开始查询,请稍等...\n\n'+'-'*30) print(' 小型轿车\n') for i in range(1, sheet.nrows): self.check(sheet.cell_value(i,0), sheet.cell_value(i,1), sheet.cell_value(i,2), '02') print('-'*30) print('\n\n 大型汽车\n') for i in range(1, sheet.nrows): self.check(sheet2.cell_value(i,0), sheet2.cell_value(i,1), sheet2.cell_value(i,2), '01') print('-'*30) input('\n\n查询结束,按Enter键结束。') car = xlrd.open_workbook(r'car.xlsx') sheet = car.sheet_by_name('小车') sheet2 = car.sheet_by_name('大车') check =Check() check.run()