利用空餘時間完成了一個對當日東方財富交易跟蹤的程式,可以將當日完成的交易利用釘釘機器人API傳送至聊天。
import json
import requests
from datetime import datetime
year = datetime.now().year
month = datetime.now().month
day = datetime.now().day
url = 'https://jywg.18.cn/Search/GetDealData?validatekey=e2fbd767-57a7-4ae1-84b7-7048f9f874d6'
while datetime(year, month, day, 9, 25) < datetime.now() < datetime(year, month, day, 11, 30) or \
datetime(year, month, day, 13, 0) < datetime.now() < datetime(year, month, day, 18, 0):
d = json.loads(requests.post(url, ).content.decode())['Data']
message = "當前時間為:{},".format(str(datetime.now())[:19])
for i in d:
cjsj = i['Cjsj'][:2] + ":" + i['Cjsj'][2:4]
if i['Mmsm'] == '證券買入' and str(datetime.now())[11:16] == cjsj:
message += '本次買入的股票程式碼是:{},買入的時間是:{}。 '.format(i['Zqdm'], cjsj)
elif i['Mmsm'] == '證券賣出' and str(datetime.now())[11:16] == cjsj:
message += '本次賣出的股票程式碼是:{},賣出的時間是:{}。 '.format(i['Zqdm'], cjsj)
if len(message) > 30:
print(message)
try_count = 0
while True:
try_count += 1
try:
msg = {
"msgtype": "text",
"text": {"content": 'Msg:交易訊號:' + message + '\n' + datetime.now().strftime("%m-%d %H:%M:%S")}}
headers = {"Content-Type": "application/json;charset=utf-8"}
url = 'https://oapi.dingtalk.com/robot/send?access_token='
body = json.dumps(msg)
requests.post(url, data=body, headers=headers)
print('釘釘已傳送')
break
except Exception as e:
if try_count:
print("傳送釘釘失敗:", e)
break
else:
print("傳送釘釘報錯,重試:", e)
本作品採用《CC 協議》,轉載必須註明作者和本文連結