-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsavestockdaily.py
More file actions
102 lines (85 loc) · 3.08 KB
/
savestockdaily.py
File metadata and controls
102 lines (85 loc) · 3.08 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
93
94
95
96
97
98
99
100
101
102
#! /usr/bin/env python
#coding=utf-8
import urllib
import urllib2
import csv
import re
import sys,traceback
import os
def loadStockDailyFromGtimg(stock_code,year):
url="http://data.gtimg.cn/flashdata/hushen/daily/"+year+"/"+stock_code+".js"
print "url="+url
req=urllib2.Request(url)
try:
res_data=urllib2.urlopen(req)
res_text=res_data.read()
#print(res_text)
pattern = re.compile(r"""(\d{6})\s{1,3}(\d*\.\d*)
\s{1,3}(\d*\.\d*)\s{1,3}(\d*\.\d*)
\s{1,3}(\d*\.\d*)\s{0,3}(\d*)
""",re.X)
all_datas=re.findall(pattern,res_text)
return all_datas
except Exception,e:
print e
#traceback.print_exc()
#print sys.exc_info()[0],sys.exc_info()[1]
print '***********no data!****************'
return
#保存数据到csv文件
def saveAnStockDailyToCsvFile(stock_code,year):
#stock_code='sh601601'
# year="13"
basepath=os.path.dirname(__file__)
stockpath=os.path.join(basepath,"stocks")
if not os.path.exists(stockpath):
os.mkdir(stockpath)
stock_File=os.path.join(stockpath, stock_code+"_"+year+".csv")
all_datas=loadStockDailyFromGtimg(stock_code,year)
if(all_datas is None):
print stock_File+"no data."
return
else:
#csvfile = file(stock_File, 'wb')
output = open(stock_File, 'wb')
csvwrite=csv.writer(output)
try:
csvwrite.writerows(all_datas)
#output.writelines(all_datas)
#z=0
#for x in all_datas:
# trade_date, open_price,close_price,high_price,low_price,volume=x
#print trade_date, open_price,close_price,high_price,low_price,volume
# output.write(trade_date+","+open_price+"," \
# +close_price+","+high_price+","+low_price+","+volume+"\n")
#raw_input_A = raw_input("raw_input: ")
print stock_File+" save success."
finally:
output.close();
#保存数据到txt文件
def saveAnStockDailyToTxtFile(stock_code,year):
#stock_code='sh601601'
# year="13"
basepath=os.path.dirname(__file__)
stockpath=os.path.join(basepath,"stocks")
if not os.path.exists(stockpath):
os.mkdir(stockpath)
stock_File=os.path.join(stockpath, stock_code+"_"+year+".txt")
all_datas=loadStockDailyFromGtimg(stock_code,year)
if(all_datas is None):
print stock_File+"no data."
return
else:
output = open(stock_File, 'w')
try:
#output.writelines(all_datas)
z=0
for x in all_datas:
trade_date, open_price,close_price,high_price,low_price,volume=x
#print trade_date, open_price,close_price,high_price,low_price,volume
output.write(trade_date+","+open_price+"," \
+close_price+","+high_price+","+low_price+","+volume+"\n")
#raw_input_A = raw_input("raw_input: ")
print stock_File+"data save success."
finally:
output.close();