Skip to content

Commit b991314

Browse files
committed
基金数据分析新增保存csv文件
1 parent 94ec15d commit b991314

1 file changed

Lines changed: 39 additions & 5 deletions

File tree

fund_analysis.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import time
66
import json
77
import os
8-
8+
import csv
9+
import sys
10+
reload(sys)
11+
sys.setdefaultencoding('utf-8')
912

1013
# app接口
1114
url = 'http://fundex2.eastmoney.com/FundWebServices/MyFavorInformation.aspx?t=kf&s=desc&sc=&pstart=0&psize=10000'
@@ -122,24 +125,55 @@ def run_write():
122125
save_json(item_dict)
123126

124127

128+
def save_csv(code):
129+
"""
130+
读取json数据,保存csv文件
131+
"""
132+
file_path = 'static/csv/'
133+
if not os.path.isdir(file_path):
134+
os.mkdir(file_path)
135+
# 创建CSV文件
136+
csv_file_name = file_path + str(code) + '.csv'
137+
csv_file = file(csv_file_name, 'wb')
138+
writer = csv.writer(csv_file)
139+
writer.writerow(['记录时间', '基金编号', '基金名称', '基金类型', '估值时间', '估算净值', '估算涨幅', '净值日期', '单位净值', '日增长率'])
140+
# 读取json数据文件
141+
filename = 'static/json/' + str(code) + '.json'
142+
with open(filename, 'r') as files:
143+
for each_line in files:
144+
item = json.loads(each_line)
145+
item_tuple = (
146+
item[u'记录时间'],
147+
item[u'基金编号'],
148+
item[u'基金名称'],
149+
item[u'基金类型'],
150+
item[u'估值时间'],
151+
item[u'估算净值'],
152+
item[u'估算涨幅'],
153+
item[u'净值日期'],
154+
item[u'单位净值'],
155+
item[u'日增长率']
156+
)
157+
writer.writerow(item_tuple)
158+
csv_file.close()
159+
160+
125161
if __name__ == "__main__":
126162
# run_write()
127163
# run_read()
128-
timed_task(run_write, 60*2)
164+
# timed_task(run_write, 60*2)
165+
save_csv('161024')
129166

130167

131168
"""
132169
因为只有app接口数据实时更新
133170
这里采用app接口作为数据来源
134171
[使用Fiddler捕获接口即可]
135-
136172
fc=100056,161024
137173
9点-16点
138174
整个过程9小时
139175
2分钟抓取一次
140176
9*60/2 = 270次/天
141-
142177
# 100056.json 名称为基金编号
143-
144178
每2分钟 追加写入
145179
"""

0 commit comments

Comments
 (0)