-
Notifications
You must be signed in to change notification settings - Fork 0
/
fund_net_value_crawler.py
62 lines (53 loc) · 2.2 KB
/
fund_net_value_crawler.py
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
#!/usr/bin/env python
from urllib2 import urlopen
import os
import json
import argparse
def get_url(url_base, arg_dict):
return "%s?%s" % (url_base, "&".join(["%s=%s" % (k, str(v)) for k, v in arg_dict.items()]))
def get_data(url):
return urlopen(url).read()
def get_net_value(fund_number, begin_date='', end_date='', force=False):
if os.path.isfile('./%s.csv' % fund_number) and not force:
with open('./%s.csv' % fund_number, 'r') as f:
data = [[t.strip() for t in l.split(',')] for l in f.readlines()]
titles = data[0]
return sorted([dict(zip(titles, net_value)) for net_value in data[1:]], key=lambda x: x['fbrq'])
url_base = 'http://stock.finance.sina.com.cn/fundInfo/api/openapi.php/CaihuiFundInfoService.getNav'
args = {
'symbol': fund_number,
'datefrom': begin_date,
'dateto': end_date,
'page': 1,
'num': 1000
}
data = get_data(get_url(url_base, args))
total_num = int(json.loads(data)['result']['data']['total_num'])
print(total_num)
page_size = args['num']
page_len = (total_num + page_size - 1) / page_size
net_values = []
for i in range(page_len):
args['page'] = i + 1
data = get_data(get_url(url_base, args)).decode('gbk')
data = json.loads(data)['result']['data']['data']
net_values += data
# print("\n".join([",".join(["%s=%s" % (k, v) for k, v in net_value.items()]) for net_value in net_values]))
titles = sorted(list(set([k for l in net_values for k in l])))
with open("./%s.csv" % fund_number, 'w') as f:
f.write(", ".join(titles))
f.write('\n')
f.write('\n'.join(", ".join([str(net_value.get(k, "")) for k in titles]) for net_value in net_values))
return net_values
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-n', "--fund_number", type=str)
parser.add_argument("all")
args = parser.parse_args()
if args.all:
from funds_crawler import crawler_all_fund
funds = crawler_all_fund(force=True)
for fund in funds:
get_net_value(fund_number=fund['symbol'],force=True)
else:
get_net_value(args.fund_number, force=True)