forked from Rockyzsu/stock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealtme_jjjz.py
More file actions
92 lines (76 loc) · 2.63 KB
/
realtme_jjjz.py
File metadata and controls
92 lines (76 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*-coding=utf-8-*-
# @Time : 2020/2/27 14:05
# @File : realtme_jjjz.py
# 实时基金净值
import datetime
import json
import random
import re
import requests
import time
from settings import get_mysql_conn, llogger
import tushare as ts
headers = {
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh,en;q=0.9,en-US;q=0.8',
}
def update_jj(table):
# table='2020-02-25' # 用于获取code列
conn = get_mysql_conn('db_fund', local='local')
# today=datetime.datetime.now().strftime('%Y-%m-%d')
# print(today)
logger = llogger(f'{table}_realtime.log')
query = 'select `基金代码`,`基金简称`,`实时价格` from `{}`'.format(table)
# print(query)
cursor = conn.cursor()
cursor.execute(query)
session = requests.Session()
ret = cursor.fetchall()
url = 'http://web.ifzq.gtimg.cn/fund/newfund/fundSsgz/getSsgz?app=web&symbol=jj{}&_var=LOAD_1582735233556_37'
add_column1 = 'alter table `{}` add column `实时净值` float'.format(table)
add_column2 = 'alter table `{}` add column `溢价率` float'.format(table)
update_sql = 'update `{}` set `实时净值`= %s,`溢价率`=%s where `基金代码`=%s'.format(table)
try:
cursor.execute(add_column1)
except Exception as e:
conn.rollback()
else:
conn.commit()
try:
cursor.execute(add_column2)
except Exception as e:
conn.rollback()
else:
conn.commit()
for item in ret:
# pass
# print(item[0])
code = item[0]
realtime_price = item[2]
s_resp = session.get(url.format(code), headers=headers)
# print(s_resp.text)
print()
print(code)
content = re.search('LOAD_\d+_\d+=(.*)', s_resp.text).group(1)
# print(content)
js = json.loads(content)
# print(js)
try:
data_list = js.get('data').get('data')
except Exception as e:
continue
# print(data_list)
last_one = data_list[-1]
time_ = last_one[0]
jj_ = last_one[1]
yjl = -1*round((jj_ - realtime_price) / realtime_price * 100, 2)
print(f'溢价率-{yjl}')
cursor.execute(update_sql, (jj_, yjl, code))
conn.commit()
conn.close()