项目概述:从网页上爬取股票信息导入本地数据库。设计 GUI 图形界面,可以在图形界面中设定组合查询的条件,即特定融资增减比例等,在数据库中实行数据检索功能,找到与之匹配的信息,并图像显示出在最近一年内个融资余额等。
以下代码为数据库处理部分
数据库资源链接:https://download.csdn.net/download/qq_45570991/20385460
import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt
import pandas as pd
import pymysql
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
plt.rcParams['font.sans-serif'] = ['SimHei']
a = '0'
stock_name=[]
con = pymysql.connect(host='localhost',database='stock',user='root',password='***',port=3306 ,charset='utf8')
cursor = con.cursor()
class myWindow:###类名称
def __init__(self, root, myTitle, Xlist):#######构造方法,##########传参root,myTitle,Xlist
self.top = tk.Toplevel(root, width=300, height=200)
self.top.title(myTitle)
self.top.attributes('-topmost', 1)
labelx = tk.Label(self.top, text='查询到%d支' % (len(Xlist)-1))
labelx.place(x=0, y=20)
buptstockpull = ttk.Combobox(self.top, width=12)
buptstockpull['values'] = Xlist # 设置下拉列表的值
buptstockpull.current(0)
buptstockpull.place(x=80, y=20) # 设置其在界面中出现的位置 column代表列 row 代表行
query_pull = ttk.Button(self.top, text="数据查询:", command=lambda: stock_pull(buptstockpull.get()))
query_pull.place(x=200, y=18)
def deleteDuplicatedElementFromList2(list):
resultList = []
for item in list:
if not item in resultList:
resultList.append(item)
return resultList
def init():
sql_2 = "SELECT stockname from ye;"
cursor.execute(sql_2)
results = cursor.fetchall()
for x in results:
stock_name.append(x)
def clickMe():
sql_1 = "SELECT * from ye where stockcode ='%s';"
cursor.execute(sql_1 % nameEntered.get())
results = cursor.fetchall()
sql_go = "SELECT * from gdhs where stockcode ='%s';"
cursor.execute(sql_go % nameEntered.get())
results0 = cursor.fetchall()
price = []
date = []
dateor = []
gdhs = []
number = 0
finace = [] # 40/30358248.8
for row in results:
price.append(float(row[3]))
date.append(row[2])
i = 0
number = 0
j = 1
while (i < 3):
if i < 2:
number += (float(row[4][i]) * 10 ** j) # 数据为几十亿,取前三位,后面单位由元转换为十亿
i = i + 1
j = j - 1
else:
number += (float(row[4][i]) / 10)
i = i + 1
j = j - 1
finace.append(number)
for row0 in results0:
m = 0
num = 0
n = 1
dateor.append(row0[2])
while (m < 3):
if m < 2:
num += (float(row0[3][m]) * 10 ** n) # 数据为几十亿,取前三位,后面单位由元转换为十亿
m = m + 1
n = n - 1
else:
num += (float(row0[3][m]) / 10)
m = m + 1
n = n - 1
gdhs.append(num)
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(date, price, 'black')
ax2.plot(date, finace, 'brown')
ax2.plot(dateor, gdhs, 'yellow')
ax1.set_xlabel('日期')
ax1.set_ylabel('收盘价(单位/元) 黑色曲线')
ax2.set_ylabel('融资融券余额(单位/亿) 棕色曲线')
ax1.set_title('%s 收盘价/融资融券余额' % row[0])
plt.show()
def Combination_query():
evaluestockprice = 0
evaluestockfinace = 0
evaluestocknum = 0
if stockprincechange.get() == '上涨10%':
evaluestockprice = 0.1
elif (stockprincechange.get() == '上涨20%'):
evaluestockprice = 0.2
elif (stockprincechange.get() == '上涨30%'):
evaluestockprice = 0.3
elif (stockprincechange.get() == '下跌10%'):
evaluestockprice = -0.1
elif (stockprincechange.get() == '下跌20%'): # 用字典可以优化
evaluestockprice = -0.2
elif (stockprincechange.get() == '下跌30%'):
evaluestockprice = -0.3
if (financing.get() == '增加10%'):
evaluestockfinace = 0.1
elif (financing.get() == '增加20%'):
evaluestockfinace = 0.2
elif (financing.get() == '增加30%'):
evaluestockfinace = 0.3
elif (financing.get() == '减少10%'):
evaluestockfinace = -0.1
elif (financing.get() == '减少20%'):
evaluestockfinace = -0.2
elif (financing.get() == '减少30%'):
evaluestockfinace = -0.3
if (numchange.get() == '增加10%'):
evaluestocknum = 0.1
elif (numchange.get() == '增加20%'):
evaluestocknum = 0.2
elif (numchange.get() == '增加30%'):
evaluestocknum = 0.3
elif (numchange.get() == '减少10%'):
evaluestocknum = -0.1
elif (numchange.get() == '减少20%'):
evaluestocknum = -0.2
elif (numchange.get() == '减少30%'):
evaluestocknum = -0.3
print(evaluestockfinace)
print(evaluestockprice)
sql_combition = "select * from ye where date between '%s' and '%s'"
cursor.execute(sql_combition % (startdate.get(), finaldate.get()))
#print(startdate.get())
#print(finaldate.get())
results = cursor.fetchall()
sql_combition2 = "select * from gdhs where Enddate between '%s' and '%s'"
cursor.execute(sql_combition2 % (startdate.get(), finaldate.get()))
resultsnum = cursor.fetchall()
#print(results)
i = 0
k = 0
name = []
stname = []
namesave = []
code = []
codesave = []
pr = []
prsave = []
fin = []
finsave =[]
finacehash = []
if (evaluestockprice > 0):
while (i < len(results) - 1):
pricegap = (float(results[i + 1][3]) - float(results[i][3])) / float(results[i][3])
#print(results[i+1][7]+' '+results[i][7])
finacegap = (float(results[i + 1][4]) - float(results[i][4])) / float(results[i][4])
#print(finacegap)
if (pricegap > evaluestockprice):
name.append(results[i][0])
code.append(results[i][1])
pr.append(results[i][3])
fin.append(results[i][4])
#print(results[i + 1][7] + ' ' + results[i][7])
finacehash.append(finacegap)
i = i + 2
elif (evaluestockprice < 0):
while (i < len(results) - 1):
pricegap = (float(results[i + 1][3]) - float(results[i][3])) / float(results[i][3])
finacegap = (float(results[i + 1][4][0:3]) - float(results[i][4][0:3])) / float(results[i][4][0:3])
if (pricegap < evaluestockprice):
name.append(results[i][0])
code.append(results[i][1])
pr.append(results[i][3])
fin.append(results[i][4])
finacehash.append(finacegap)
i = i + 2
#print(stock1)
#print(finacehash)
if (evaluestocknum > 0):
while (k < len(resultsnum) - 1):
numgap = (float(resultsnum[k + 1][3]) - float(resultsnum[k][3])) / float(resultsnum[k][3])
if (numgap > evaluestocknum):
stname.append(resultsnum[k][0])
k = k + 2
elif (evaluestocknum < 0):
while (k < len(resultsnum) - 1):
numgap = (float(resultsnum[k + 1][3]) - float(resultsnum[k][3])) / float(resultsnum[k][3])
if (numgap < evaluestocknum):
stname.append(resultsnum[k][0])
k = k + 2
m = 0
q = 0
if (evaluestockfinace > 0):
while (m < len(finacehash) - 1):
if (finacehash[m] < evaluestockfinace):
name[m] = '0'
code[m] = '0'
pr[m] = '0'
fin[m] = '0'
m = m + 1
elif (evaluestockfinace < 0):
while (m < len(finacehash) - 1):
if (finacehash[m] > evaluestockfinace):
name[m] = '0'
code[m] = '0'
pr[m] = '0'
fin[m] = '0'
m = m + 1
while '0' in name:
name.remove('0')
code.remove('0')
pr.remove('0')
fin.remove('0')
#print(stock1)
for x in range(0,len(name)-1):
for y in range(0,len(stname)-1):
if(name[x] == stname [y]):
namesave.append(name[x])
codesave.append(code[x])
prsave.append(pr[x])
finsave.append(fin[x])
w1 = myWindow(win, '查询结果如下', namesave)
t = 0
while (t < len(namesave) - 1):
text.insert('insert', t+1)
text.insert('insert', '\t\t')
text.insert('insert', codesave[t])
text.insert('insert', '\t\t')
text.insert('insert', namesave[t])
text.insert('insert', '\t\t')
text.insert('insert', prsave[t])
text.insert('insert', '\t\t')
text.insert('insert', finsave[t])
text.insert('insert','\n')
t = t + 1
def stock_pull(a):
sql_1 = "SELECT * from ye where stockname ='%s';"
cursor.execute(sql_1 % a)
results = cursor.fetchall()
price = []
date = []
number = 0
finace = []
for row in results:
price.append(float(row[3]))
date.append(row[2])
i = 0
number = 0
j = 1
while (i < 3):
if i < 2:
number += (float(row[4][i]) * 10 ** j) # 数据为几十亿,取前三位,后面单位由元转换为十亿
i = i + 1
j = j - 1
else:
number += (float(row[4][i]) / 10)
i = i + 1
j = j - 1
finace.append(number)
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(date, price, 'g-')
ax2.plot(date, finace, 'b-')
ax1.set_xlabel('日期')
ax1.set_ylabel('收盘价(单位/元) 绿色曲线')
ax2.set_ylabel('融资融券余额(单位/亿) 蓝色曲线')
ax1.set_title('%s 收盘价/融资融券余额' % row[0])
plt.show()
win = tk.Tk()
win.title("股票分析系统")
win.geometry('900x500+150+100')
label1 = ttk.Label(win, text="请输入你要查询的股票代码:")
label1.grid(row=0, column=0)
nameEntered = tk.Entry(win, width=10)
nameEntered.grid(row=0, column=1)
nameEntered.focus()
query_stock_code = ttk.Button(win, text="查询", command=clickMe)
query_stock_code.grid(row=0, column=2)
label2 = ttk.Label(win, text="进行组合查询:")
label2.grid(row=4, column=0)
label3 = ttk.Label(win, text="输入查询起始日期")
label3.grid(row=4, column=1)
label4 = ttk.Label(win, text="输入查询截止日期")
label4.grid(row=5, column=1)
label5 = ttk.Label(win, text="融资增减比例")
label5.grid(row=6, column=1)
label6 = ttk.Label(win, text="股价涨跌幅")
label6.grid(row=7, column=1)
label7 = ttk.Label(win, text="股东户数增减比例")
label7.grid(row=8, column=1)
label10 = ttk.Label(win, text="显示查询结果如下表所示")
label10.grid(row=15, column=1)
startdate = tk.Entry(win, width=18)
startdate.grid(row=4, column=2)
startdate.insert(19, "2019-01-11 00:00:00")
finaldate = tk.Entry(win, width=18)
finaldate.grid(row=5, column=2)
finaldate.insert(19, '2020-05-20 00:00:00')
financing = ttk.Combobox(win, width=12)
financing['values'] = ["增加10%", "增加20%", "增加30%", "减少10%", "减少20%", "减少30%"] # 设置下拉列表的值
financing.grid(row=6, column=2) # 设置其在界面中出现的位置 column代表列 row 代表行
financing.current(0)
stockprincechange = ttk.Combobox(win, width=12)
stockprincechange['values'] = "上涨10%", "上涨20%", "上涨30%", "下跌10%", "下跌20%", "下跌30%" # 设置下拉列表的值
stockprincechange.grid(row=7, column=2) # 设置其在界面中出现的位置 column代表列 row 代表行
stockprincechange.current(0)
numchange = ttk.Combobox(win, width=12)
numchange['values'] = ["增加10%", "增加20%", "增加30%", "减少10%", "减少20%", "减少30%"]# 设置下拉列表的值
numchange.grid(row=8, column=2) # 设置其在界面中出现的位置 column代表列 row 代表行
numchange.current(0)
label7 = ttk.Label(win, text="进行组合查询")
label7.grid(row=10, column=1)
query_stock = ttk.Button(win, text="查询:", command=Combination_query)
query_stock.grid(row=10, column=2)
# 创建滚动条
scroll = tk.Scrollbar()
# 创建文本框text,设置宽度100,high不是高度,是文本显示的行数设置为3行
text = tk.Text(win)
# 将滚动条填充
scroll.grid(row=20, column=3, sticky=tk.NS) # side是滚动条放置的位置,上下左右。fill是将滚动条沿着y轴填充
text.grid(row=20, column=1, sticky=tk.NS) # 将文本框填充进窗口的左侧,
# 将滚动条与文本框关联
scroll.config(command=text.yview) # 将文本框关联到滚动条上,滚动条滑动,文本框跟随滑动
text.config(yscrollcommand=scroll.set) # 将滚动条关联到文本框
win.mainloop()