在前面的文章中,我们一起学习了如何通过 Python 抓取东方财富网的实时股票数据,链接如下 用 Python 爬取股票实时数据 今天我们就在这个基础上,实现一个 Tkinter GUI 程序,完
在前面的文章中,我们一起学习了如何通过 Python 抓取东方财富网的实时股票数据,链接如下
用 Python 爬取股票实时数据
今天我们就在这个基础上,实现一个 Tkinter GUI 程序,完成无代码股票抓取!
首先对于 Tkinter 相信大家都是比较了解的,如果有小伙伴对于 Tkinter 的相关用法不是特别熟悉的话,可以看如下文章
Tkinter 入门之旅
首先我们先看一下 GUI 程序的最终效果
该程序共分三个区域:
- 个股查询:用于查询某只股票的数据,可以查询1天或者多天数据
- 批量查询:查询某个交易所所有股票的数据
- 日志区域:打印抓取信息
下面我们就来看看该如何从头完成这个 GUI 程序
程序布局
首先就是程序布局,这里我们使用了 ttkbootstrap 库来美化整体程序
程序初始化部分
import ttkbootstrap as ttkfrom ttkbootstrap.constants import *
import tkinter.messagebox as messbox
class MainCreator(ttk.Window):
def __init__(self):
super().__init__("股票抓取工具", themename="solar", resizable=(False, False))
self.configure_frame = ttk.Frame(self, padding=(10, 10, 5, 10))
self.configure_frame.pack(side=LEFT, fill=BOTH, expand=YES)
self.demo_frame = ttk.Frame(self, padding=(5, 10, 10, 10))
self.demo_frame.pack(side=LEFT, fill=BOTH, expand=YES)
程序布局部分
def create_frame(self):"""Create all the frame widgets"""
container = ttk.Frame(self)
container.pack(side=LEFT, fill=BOTH, expand=YES, padx=5)
color_group = ttk.Labelframe(
master=container, text="个股查询", padding=10
)
color_group.pack(fill=X, side=TOP)
en_command = super().register(self.en_validate)
self.en0 = ttk.Entry(color_group, width=5, text='', bootstyle='warning',
validate='key', validatecommand=(en_command, '%P'))
self.en0.insert('0', 1)
self.en0.config(state=DISABLED)
...
总体上来说,我们所有的组件都是从 ttk 当中实例化的,也就是直接复用了库 ttkbootstrap 的相关美化功能,使得我们的程序看起来更加高级美观
抓取与保存功能
下面我们编写股票抓取代码和对应的保存代码
股票抓取
def get_A_mins(code):if code.startswith("3") or code.startswith("0"):
url = shang_A_url.replace("%s", code)
elif code.startswith("6"):
url = shen_A_url.replace("%s", code)
else:
return False
res = requests.get(url)
result = res.text.split("cb_1659146437934_51841953")[1].split("(")[1].split(");")[0]
result_json = json.loads(result)
stock_data = result_json['data']
return stock_data
def get_A_days(code):
if code.startswith("3") or code.startswith("0"):
url = shang_A_days.replace("%s", code)
elif code.startswith("6"):
url = shen_A_days.replace("%s", code)
else:
return False
res = requests.get(url)
result = res.text.split("cb_1659171393020_15037673")[1].split("(")[1].split(");")[0]
result_json = json.loads(result)
stock_data = result_json['data']
return stock_data
def get_hsj(date):
total_data = []
try:
for i in range(1, 5):
res = requests.get(hsj_url.replace("%s", str(i)))
result = res.text.split("jQuery112402508937289440778_1658838703304")[1].split("(")[1].split(");")[0]
result_json = json.loads(result)
stock_data = result_json['data']
if stock_data:
total_data.append(stock_data)
saveFunc.save_data_hsj(stock_data['diff'], date)
else:
return total_data
return total_data
except Exception as e:
return False
def get_Center():
total_data = []
for i in range(1, 20):
res = requests.get(center_url.replace("%s", str(i)))
result = res.text.split("jQuery112404177389105264733_1659176039486")[1].split("(")[1].split(");")[0]
result_json = json.loads(result)
center_data = result_json['data']
if center_data:
total_data.append(center_data)
else:
return total_data
def get_shang_A():
pass
def get_shen_A():
pass
def get_bei_A():
pass
股票代码分为上交所,深交所和北交所以及大盘行情数据,所以我们分别编写函数进行处理
数据保存
import osdef save_data_mins(data, date):
Code = data['code']
Name = data['name']
if not os.path.exists(r"stock_data_%s_%s_%s_mins.csv" % (Code, Name, date)):
with open("stock_data_%s_%s_%s_mins.csv" % (Code, Name, date), "a+", encoding='utf-8') as f:
f.write("时间,最新价,成交量(手),成交额\n")
for i in data['trends']:
i_list = i.split(",")
time = i_list[0]
price = i_list[2]
mount = i_list[5]
count = i_list[6]
row = '{},{},{},{}'.format(
time,price,mount,count)
f.write(row)
f.write('\n')
else:
...
def save_data_days(data, days):
print(days)
Code = data['code']
Name = data['name']
if not os.path.exists(r"stock_data_%s_%s_days.csv" % (Code, Name)):
with open("stock_data_%s_%s_days.csv" % (Code, Name), "a+", encoding='utf-8') as f:
f.write("时间,开盘价,收盘价,最高价,最低价,成交量(手),成交额,振幅,涨跌幅,涨跌额,换手率\n")
for i in data["klines"][::-1][:int(days)]:
i_list = i.split(",")
time = i_list[0]
Open = i_list[1]
close = i_list[2]
heigh = i_list[3]
low = i_list[4]
mount = i_list[5]
count = i_list[6]
amplitude = i_list[7]
changePercent = i_list[8]
change = i_list[9]
turnoverRate = i_list[10]
row = '{},{},{},{},{},{},{},{},{},{},{}'.format(
time,Open,close,heigh,low,mount,count,amplitude,changePercent,change,turnoverRate)
f.write(row)
f.write('\n')
else:
...
def save_data_hsj(data, date):
if not os.path.exists(r'stock_data_%s.csv' % date):
with open("stock_data_%s.csv" % date, "a+", encoding='utf-8') as f:
f.write("股票代码,股票名称,最新价,涨跌幅,涨跌额,成交量(手),成交额,振幅,换手率,市盈率,量比,最高,最低,今开,昨收,市净率\n")
for i in data:
Code = i["f12"]
Name = i["f14"]
Close = i['f2']
ChangePercent = i["f3"]
Change = i['f4']
Volume = i['f5']
Amount = i['f6']
Amplitude = i['f7']
TurnoverRate = i['f8']
PERation = i['f9']
VolumeRate = i['f10']
Hign = i['f15']
Low = i['f16']
Open = i['f17']
PreviousClose = i['f18']
PB = i['f22']
row = '{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}'.format(
Code,Name,Close,ChangePercent,Change,Volume,Amount,Amplitude,
TurnoverRate,PERation,VolumeRate,Hign,Low,Open,PreviousClose,PB)
f.write(row)
f.write('\n')
else:
...
def save_data_center():
pass
def save_data_shang_A():
pass
添加功能
接下来就是为布局好的 GUI 程序添加各种响应功能
个股查询按钮
为个股查询的抓取按钮绑定方法catch
self.bt = ttk.Button(color_group, text='抓取', bootstyle='success', command=self.catch)方法catch的定义如下
def catch(self):self.txt.yview_moveto(1)
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
day = datetime.datetime.now().strftime('%Y-%m-%d')
if self.cbvar.get() == 1:
catch_day = self.en0.get()
if self.en.get():
code = self.en.get()
result = catchFunc.get_A_days(code)
if result:
saveFunc.save_data_days(result, catch_day)
self.txt.insert(ttk.INSERT, "抓取股票%s成功 %s" % (code, now))
self.txt.insert(ttk.INSERT, "\n")
self.txt.update()
self.txt.yview_moveto(1)
else:
print("股票代码错误")
messbox.showerror("股票代码错误", "请输入正确的股票代码!")
else:
print("请输入股票代码")
messbox.showerror("股票代码为空", "请输入股票代码!")
else:
...
批量查询开关
对于批量查询,我们是通过一个多选框开关控制的
self.cb_batch = ttk.Checkbutton(cr_group, text="开启批量", variable=self.cbvar1, command=self.cb_button)绑定的方法如下
def cb_button(self):if self.cbvar1.get() == 1:
self.bt_batch.config(state=NORMAL)
else:
self.bt_batch.config(state=DISABLED)
以上就是程序的部分代码,让我们看一下整体效果吧
好了,以上就是今天分享的全部内容,喜欢就点个赞吧~
文章点赞+在看,私聊获取完整代码