Skip to content

Commit

Permalink
provide command line interface for user.
Browse files Browse the repository at this point in the history
  • Loading branch information
terryding77 committed Jul 29, 2016
1 parent 0b875be commit 0a0f342
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# sina_fund_crawler
craw China fund information by Sina website

##Usage:
1. please ensure python2.7 environment on your computer.
2. execute `python funnds_crawler.py` , we will get **funds.csv** at your working directory
3. choose a fund which you want to get it's net value list, and execute `python fund_net_value_crawler.py -p fund_number(like 519007)`, when process terminated, we will get **fund_number.csv (like 519007.csv)** at your working directory.


15 changes: 10 additions & 5 deletions fund_net_value_crawler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python
from urllib2 import urlopen
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()]))
Expand Down Expand Up @@ -28,17 +30,20 @@ def get_net_value(fund_number, begin_date='', end_date=''):
for i in range(page_len):
args['page'] = i + 1
data = get_data(get_url(url_base, args)).decode('gbk')
print(data)
data = json.loads(data)['result']['data']['data']
print(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]))
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:
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__":
get_net_value('590008')
parser = argparse.ArgumentParser()
parser.add_argument('-n', "--fund_number", required=True, type=str)
args = parser.parse_args()
get_net_value(args.fund_number)

3 changes: 2 additions & 1 deletion funds_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ def crawler_all_fund():

titles = sorted(list(set([k for l in funds for k in l])))
print(titles)
with open('funds.csv', 'w') as f:
with open('./funds.csv', 'w') as f:
f.write(", ".join(titles))
f.write('\n')
for l in funds:
print(", ".join(["%s = %s" % (k,v) for k, v in l.items()]))
f.write(", ".join([unicode(l.get(k, "")).encode("gbk") for k in titles]))
f.write('\n')
return funds


if __name__ == '__main__':
Expand Down

0 comments on commit 0a0f342

Please sign in to comment.