共计 5710 个字符,预计需要花费 15 分钟才能阅读完成。
在后面的文章中,咱们一起学习了如何通过 Python 抓取西方财富网的实时股票数据,链接如下
用 Python 爬取股票实时数据
明天咱们就在这个根底上,实现一个 Tkinter GUI 程序,实现无代码股票抓取!
首先对于 Tkinter 置信大家都是比拟理解的,如果有小伙伴对于 Tkinter 的相干用法不是特地相熟的话,能够看如下文章
Tkinter 入门之旅
首先咱们先看一下 GUI 程序的最终成果
该程序共分三个区域:
- 个股查问:用于查问某只股票的数据,能够查问 1 天或者多天数据
- 批量查问:查问某个交易所所有股票的数据
- 日志区域:打印抓取信息
上面咱们就来看看该如何从头实现这个 GUI 程序
程序布局
首先就是程序布局,这里咱们应用了 ttkbootstrap
库来丑化整体程序
程序初始化局部
import ttkbootstrap as ttk
from 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 os
def 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)
好了,以上就是程序的局部代码,让咱们看一下整体成果吧
以上就是明天分享的全部内容,喜爱就点个赞吧~
文章点赞 + 在看,私聊获取残缺代码
本文由 mdnice 多平台公布
正文完