使用python实现的天眼查小工具
此文章已同步更新至我的个人博客https://simonting.gitee.io
前言
帮老同学写的一个python脚本,具体需求主要是绘制一个面板,连接数据库,输入公司名称关键字,查询满足要求的数据,并且带有导出excel功能。数据库中的数据来源于https://blog.csdn.net/King__Cheung/article/details/109484246 这篇博客中通过python脚本调用企查查接口所获取到的数据。
一、代码
#!/usr/bin/python3
# -*- coding: UTF-8 -*-# pip3 install PyMySQLimport tkinter as tk
from tkinter import messagebox
import pymysql
import csv
import oswindow = tk.Tk()
# 标题
window.title('天眼查小工具')
# 窗口大小
window.geometry('900x500+500+250')
# 请输入关键字提示文字
label1 = tk.Label(window, text="请输入关键字:")
# 下方提示文字
label2 = tk.Label(window, text="输入关键字点击查询按钮即可搜索,点击导出数据即可导出为excel文件。")
# 关键字输入框
company_name_input = tk.Entry(window, show=None, font=('微软雅黑', 14))
# 查询结果显示文本框
text = tk.Text(window, width=71, height=14, font=('微软雅黑', 15))
# 滚动条
scroll = tk.Scrollbar()
# 连接数据库
db = pymysql.connect("localhost", "root", "zhangting", "holder")
cursor = db.cursor()
success_text = "程序启动成功。\n数据库连接成功。\n输入关键字开始查询吧..."
text.insert("insert", success_text)
export_res = ()
export_file_name = "data.csv"# 清空
def reset():global export_resexport_res = ()company_name_input.delete(0, "end")text.delete(1.0, "end")# 查询
def search():text.delete(1.0, "end")key_word = company_name_input.get()key_word = str(key_word).lstrip().rstrip()get_data(key_word)# 导出
def export():if len(export_res) == 0:messagebox.showwarning('提示', '内容为空,无法导出!')returncreate_csv_file('data-1')csv_file = open(export_file_name, 'a+', newline='')try:writer = csv.writer(csv_file)header = ('股东', '公司', 'Account UserName', 'Primary UserName', 'Account Segment', 'AM Name')writer.writerow(header)for res in export_res:writer.writerow(res)abspath = os.path.abspath(export_file_name)messagebox.showinfo('提示', '文件导出完成!\n文件地址:' + abspath + "\n")finally:csv_file.close()# 生成csv文件,且防止重名
def create_csv_file(file_name):if os.path.exists(file_name + '.csv'):index = int(file_name.split('-', 1)[1]) + 1create_csv_file('data-' + str(index))else:global export_file_nameexport_file_name = str(file_name) + '.csv'# 查询按钮
button_search = tk.Button(window, text="查询", bg="LightBlue", font=('微软雅黑', 12), width=6, height=1, command=search)
# 清空按钮
button_reset = tk.Button(window, text="清空", bg="LightCyan", font=('微软雅黑', 12), width=6, height=1, command=reset)
# 导出按钮
button_export = tk.Button(window, text="导出数据", font=('微软雅黑', 12), width=8, height=1, command=export)# 查询所有数据(包含直接关联与非直接关联的数据)
def execute_sql(key_word):if len(key_word) != 0:cursor.execute('select share,name,account_username,primary_username,account_segment'',am_name from holder where share in (select share from holder where'' name like \'%' + key_word + '%\')')return cursor.fetchall()# 查询直接关联的数据
def search_direct(key_word):if len(key_word) != 0:cursor.execute('select share,name,account_username,primary_username,account_segment'',am_name from holder where name like \'%' + key_word + '%\'')return cursor.fetchall()# 查询数据、数据处理
def get_data(key_word):content = '请输入关键字!'if len(key_word) != 0:result = execute_sql(key_word)result = dig_data(result)# 直接关联的数据direct_result = search_direct(key_word)# 求出非直接关联的公司indirect_result = set(result).difference(set(direct_result))indirect_result = tuple(indirect_result)global export_resexport_res = resultif len(result) != 0:content = "共查询到" + str(len(result)) + "条与“" + key_word + "”相关的信息:\n股东" \+ " - " + "公司" + " - " + "Account UserName" \+ " - " + "Primary UserName" + " - " + "Account Segment" \+ " - " + "AM Name" + "\n"content += "------------------------------\n与“" + key_word + "”直接关联的数据为:\n"i = 1for res in direct_result:content += str(i) + "、" + res[0] + " - " + res[1] + " - " + res[2] \+ " - " + res[3] + " - " + res[4] + " - " \+ res[5] + "\n"i += 1content += "------------------------------\n与“" + key_word + "”非直接关联的数据为:\n"if len(indirect_result) != 0:for res in indirect_result:content += str(i) + "、" + res[0] + " - " + res[1] + " - " + res[2] \+ " - " + res[3] + " - " + res[4] + " - " \+ res[5] + "\n"i += 1else:content += "无数据\n"else:content = "未查询到任何与“" + key_word + "”相关的结果!"text.insert("insert", content)db.commit()# 数据挖掘
def dig_data(result):all_company = ""for res in result:all_company += res[1]data = ()for res in result:share = str(res[0])if share.endswith("公司"):data += execute_sql(share)return tuple(set(result + data))# 控件布局
def main():# "请输入关键字"提示文字坐标label1.place(x=10, y=20)# 关键字输入框坐标company_name_input.place(x=100, y=17)# 关键字输入框的长度与宽度company_name_input.place(width=590, height=30)# 查询按钮坐标button_search.place(x=720, y=12)# 清空按钮坐标button_reset.place(x=800, y=12)# 查询结果显示文本框坐标text.place(x=12, y=60)# 滚动条的位置scroll.pack(side=tk.RIGHT, fill=tk.Y)scroll.config(command=text.yview)text.config(yscrollcommand=scroll.set)# 导出按钮坐标button_export.place(x=780, y=455)# 下方提示文字坐标label2.place(x=10, y=465)window.mainloop()if __name__ == '__main__':main()
功能截图
查询功能:
- 1、 支持向下挖掘一层
- 2、支持直接关联数据与非直接关联数据分开显示
导出功能:
导出文件名支持自动重命名,不会覆盖之前导出的文件。
内容为空,无法导出:
导出的excel示例:
数据库数据示例:
使用python实现的天眼查小工具相关推荐
- Python 制作天眼查小程序
这是「进击的Coder」的第 788 篇技术分享 作者:萝卜大杂烩 来源:萝卜大杂烩 " 阅读本文大概需要 9 分钟. " 今天我们一起来制作一个天眼查 GUI 程序,开宗明义,我 ...
- python打字_使用Python制作一个打字训练小工具
一.写在前面 说道程序员,你会想到什么呢?有人认为程序员象征着高薪,有人认为程序员都是死肥宅,还有人想到的则是996和 ICU. 别人眼中的程序员:飞快的敲击键盘.酷炫的切换屏幕.各种看不懂的字符代码 ...
- python 编写实用小工具-使用Python制作一个打字训练小工具
一.写在前面 说道程序员,你会想到什么呢?有人认为程序员象征着高薪,有人认为程序员都是死肥宅,还有人想到的则是996和 ICU. 别人眼中的程序员:飞快的敲击键盘.酷炫的切换屏幕.各种看不懂的字符代码 ...
- python 24位图转 8位_Python爬取PPT模板小工具下载-Python爬取PPT模板小工具免费版下载v1.0...
由于很多PPT抓取工具都会因为版本问题无法使用,所以论坛大神就自己写了这款Python爬取PPT模板小工具,可以帮助用户轻松获取各种PPT模板,使用的时候注意一次只能下载一种类型.软件仅供交流学习,下 ...
- python制作翻译小软件_如何基于Python制作有道翻译小工具
这篇文章主要介绍了如何基于Python制作有道翻译小工具,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 该工具主要是利用了爬虫,爬取web有道翻译的 ...
- python进行高效记账的小工具
python进行高效记账的小工具 现有记账工具梳理 自己开发记账工具的思路 记账工具代码简介 1.建立3个数据库 2.与数据库的交互 3.提取数据 4.添加交易对象 5.生成财务报表 5.1高频交易对 ...
- 【Python工具】Python版本的天眼查,是不是就很nice啦 | 附带源码
相关文件 关注小编,私信小编领取哟! 当然别忘了一件三连哟~~ 公众号:Python日志 可以关注小编公众号,会不定时的发布一下Python小技巧,还有很多资源可以免费领取哟!! 源码领取:加Pyth ...
- python查询文件软件_Python小工具--多文件查找字符串fs
前几天,在看Android的makefile时,为方便自己在make文件查找字符串,用perl写了个小工具,实现的功能就是在目录下的多个文本文件中查找字符串.那个工具在同事的帮助下,已经实现了不少功能 ...
- python打字效果_Python学习之旅:用Python制作一个打字训练小工具
一.写在前面 说道程序员,你会想到什么呢?有人认为程序员象征着高薪,有人认为程序员都是死肥宅,还有人想到的则是996和 ICU. Python资源共享群:626017123 别人眼中的程序员:飞快的敲 ...
最新文章
- PTA数据结构与算法题目集(中文)7-38
- python svm超参数_grid search 超参数寻优
- 「重返18岁」!英特尔首任CTO重磅回归,以CEO身份重掌帅印
- 神级总结:七种功能强大的聊天机器人平台
- 【深度学习入门到精通系列】关于梯度下降和反向传播的探索
- Halcon图像增强方法与原理概述
- python读txt转array_python将txt文件读入为np.array的方法
- java 堆 是用_Java8元空间和堆使用
- DataGridView导出到Excel的三个方法
- 哈夫曼编码与哈夫曼树
- catia曲面扫掠命令详解_CATIA建模教程(三)——扫掠曲面在曲面造型中的应用.pdf...
- php k线公式源码,黄金K线主图源码
- 十、InnoDB的Buffer Pool
- Leetcode 006. Z 字形变换 | 每日一题
- 神武4虚拟机 进不去怎么办 无法登陆我们应如何解决?
- 简单的邮箱格式校验方式
- 微软工业计算机主机怎么样,微软Surface Pro 4到底值不值得买?
- 信号与系统——初识到理解(第二章——信号与系统)
- Java模拟ATM机系统
- pat B1032 挖掘机技术哪家强 (20分)