python获取股市股票数据并播报和右下角弹窗提示,利用datetime和win32com模块,有针对性选择大盘晴雨表卡片
#!/usr/bin/env python
# coding=utf-8
# 获取股票数据并播报和弹窗提示
import requests, re
import win32com.client
import datetime,time
from show_msg import TestTaskbarIcon
class Check():
def __init__(self):
self.speaker = win32com.client.Dispatch("SAPI.SpVOice")
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36",
"Referer":"http://finance.sina.com.cn"}
# url = 'http://hq.sinajs.cn/?format=text&list=sh603616,sz002024'
hanjian_url = 'https://hq.sinajs.cn/list=s_sh603616' # 韩建河山
suning_url = 'https://hq.sinajs.cn/list=s_sz002123' # 梦网集团
huiyuan_url = 'https://hq.sinajs.cn/list=s_sz000586' # 汇源通信
# url = 'http://finance.sina.com.cn/realstock/company/sh603616/nc.shtml?from=BaiduAladin'
self.url_list = [hanjian_url, suning_url, huiyuan_url]
self.session = requests.Session()
self.session.keep_alive = False # 关闭多余链接
# 处理check
def check(self,t):
# 当天的11:30:00时间转换成日期格式
eleven_half_time = "11:30:00"
result_eleven_half_time = datetime.datetime.strptime(eleven_half_time, "%H:%M:%S")
# 当天的13点时间转换成日期格式
thirteen_time = "13:00:00"
result_thirteen_time = datetime.datetime.strptime(thirteen_time, "%H:%M:%S")
# 当天的15点时间转换成日期格式
fifteen_time = "15:00:00"
result_fifteen_time = datetime.datetime.strptime(fifteen_time, "%H:%M:%S")
while True:
# 获取本地时间
# now_time = time.strftime("%H:%M:%S", time.localtime())
# 电脑时间快2分钟31秒,所以减去2分钟31秒
now_time = (datetime.datetime.now() - datetime.timedelta(minutes=2,seconds=31)).strftime("%H:%M:%S")
result_now_time = datetime.datetime.strptime(now_time, "%H:%M:%S")
print(result_now_time)
# 如果现在时间小于11点半或者在13点和15点之间运行
if result_now_time < result_eleven_half_time or result_thirteen_time < result_now_time < result_fifteen_time:
print("======================")
for url in self.url_list:
# response = requests.get(url, headers=self.headers)
self.session.keep_alive = False # 关闭多余连接
response = self.session.get(url, headers=self.headers)
html = response.text
# 名称
name = html.split('"')[1].split(",")[0]
# 价格
price = float(html.split('"')[1].split(",")[1])
# 当天涨幅,涨了多少
ups_down = float(html.split('"')[1].split(",")[2])
ups_down = round(ups_down,2)
# 涨跌的百分比
percent = html.split('"')[1].split(",")[3]
print(name, price)
t.showMsg(name[:2], str(price)+"("+str(ups_down)+")"+percent+"%")
self.speaker.Speak(name[:2]) # 播报名字
self.speaker.Speak(price) # 播报价格
else:
t.showMsg("不在工作时间", "退出了")
break
if __name__ == "__main__":
t = TestTaskbarIcon()
ch = Check()
ch.check(t)
弹窗模块show_msg.py: 右下角弹窗封装模块show_msg
结合支付宝大盘晴雨表改编:
#!/usr/bin/env python
# coding=utf-8
# 获取股票数据并播报和弹窗提示
import requests, re
import win32com.client
import datetime,time,random
from show_msg import TestTaskbarIcon
class Check():
def __init__(self):
self.speaker = win32com.client.Dispatch("SAPI.SpVOice")
# self.headers = {
# "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"}
self.user_agent_list = [
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
"Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36",
]
self.headers = {}
# url = 'http://hq.sinajs.cn/?format=text&list=sh603616,sz002024'
hanjian_url = 'https://hq.sinajs.cn/list=s_sh603616' # 韩建河山
mengwang_url = 'https://hq.sinajs.cn/list=s_sz002123' # 梦网集团
huiyuan_url = 'https://hq.sinajs.cn/list=s_sz000586' # 汇源通信
haicheng_url = 'https://hq.sinajs.cn/list=s_sz002116' # 中国海诚
fengle_url = 'https://hq.sinajs.cn/list=s_sz000713' # 丰乐
runda_url = 'https://hq.sinajs.cn/list=s_sh603108' # 润达医疗
gang_url = 'https://hq.sinajs.cn/list=s_sz002057' # 中钢天源
shangzheng = 'https://hq.sinajs.cn/list=sh000001' #上证指数
# url = 'http://finance.sina.com.cn/realstock/company/sh603616/nc.shtml?from=BaiduAladin'
self.url_list = [ shangzheng ,hanjian_url]
self.session = requests.Session()
self.session.keep_alive = False # 关闭多余链接
# 处理check
def check(self,t):
# 当天的11:30:00时间转换成日期格式
eleven_half_time = "11:30:00"
result_eleven_half_time = datetime.datetime.strptime(eleven_half_time, "%H:%M:%S")
# 当天的13点时间转换成日期格式
thirteen_time = "13:00:00"
result_thirteen_time = datetime.datetime.strptime(thirteen_time, "%H:%M:%S")
# 当天的15点时间转换成日期格式
fifteen_time = "15:00:00"
result_fifteen_time = datetime.datetime.strptime(fifteen_time, "%H:%M:%S")
while True:
# 获取本地时间
# now_time = time.strftime("%H:%M:%S", time.localtime())
# 电脑时间快2分钟15秒,所以减去2分钟15秒
# now_time = (datetime.datetime.now() - datetime.timedelta(minutes=4,seconds=45)).strftime("%H:%M:%S")
now_time = (datetime.datetime.now() - datetime.timedelta(minutes=0,seconds=10)).strftime("%H:%M:%S")
result_now_time = datetime.datetime.strptime(now_time, "%H:%M:%S")
# 打印时间
t2 = str(result_now_time).split(" ")[1]
print(t2)
# 如果现在时间小于11点半或者在13点和15点之间运行
if result_now_time < result_eleven_half_time or result_thirteen_time < result_now_time < result_fifteen_time:
print("======================")
for url in self.url_list:
self.headers['User-Agent'] = random.choice(self.user_agent_list)
self.headers['Referer'] = 'http://finance.sina.com.cn'
# response = requests.get(url, headers=self.headers)
self.session.keep_alive = False # 关闭多余连接
response = self.session.get(url, headers=self.headers)
html = response.text
# 名称
name = html.split('"')[1].split(",")[0]
if name == "上证指数":
# 现在值
price = float(html.split('"')[1].split(",")[3])
# 昨收值
yesterd_price = float(html.split('"')[1].split(",")[2])
# 涨跌百分比
percent = (price-yesterd_price)/yesterd_price*100
percent = round(percent, 2)
# 卡片信息
if percent >= 0.2:
print("晴天卡",percent)
card = "晴↑"
elif percent <= -0.2:
print("雨天卡",percent)
card = "雨↓"
else:
print("多云卡",percent)
if percent <0:
card = "多云↓"
else:
card = "多云↑"
percent = str(percent)
# 当天涨幅,涨了多少元
ups_down = price-yesterd_price
ups_down = round(ups_down, 2)
price = round(price, 2)
# 晴:交易日的9: 30 - 23:59之间,大盘涨幅在 + 0.2 % 以及更大涨幅时出现(包含 + 0.2 %);>=0.2
# 雨:交易日的9: 30 - 23:59 之间,大盘跌幅在 - 0.2 % 以及更大跌幅时出现(包含 - 0.2 %);<=-0.2
# 多云:交易日的9: 30 - 23:59之间,大盘涨跌范围在 - 0.20 % ~+0.20 % 之间;
t.showMsg(name[:2] + t2, str(price) + "(" + str(ups_down) + ")" + percent + "%" + card)
else:
# 价格
price = float(html.split('"')[1].split(",")[1])
# 当天涨幅,涨了多少元
ups_down = float(html.split('"')[1].split(",")[2])
ups_down = round(ups_down,2)
# 涨跌的百分比
percent = html.split('"')[1].split(",")[3]
t.showMsg(name[:2]+t2, str(price)+"("+str(ups_down)+")"+percent+"%")
print(name, price)
time.sleep(2)
# self.speaker.Speak(name[:2]) # 播报名字
# self.speaker.Speak(price) # 播报价格
else:
t.showMsg("不在工作时间", "退出了")
break
if __name__ == "__main__":
t = TestTaskbarIcon()
ch = Check()
ch.check(t)
安装 win32com模块:
在cmd中使用python -m pip install pypiwin32进行安装,成功解决