Skip to content

Instantly share code, notes, and snippets.

@xgdgsc
Created November 22, 2015 08:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xgdgsc/d8e8ab94350c0363378e to your computer and use it in GitHub Desktop.
Save xgdgsc/d8e8ab94350c0363378e to your computer and use it in GitHub Desktop.
get tushare data
# coding: utf-8
import tushare as ts
import pandas as pd
import pickle
stock_list = ts.get_stock_basics()
# In[ ]:
stock_list
# In[ ]:
code_list = []
for code in stock_list.index:
code_list.append(code)
#print(code)
io_error_list = []
value_error_list = []
attribute_error_list = []
store = pd.HDFStore('stocks_h.hdf')
keys = store.keys()
for code in code_list:
if '/code' + code in keys:
print(code+' already downloaded')
else:
try:
data = ts.get_h_data(code,start='2005-01-01',end='2015-11-19')
if data is not None:
data.to_hdf('stocks_h.hdf',
key='code'+code, complevel=9, complib='zlib')
print(code+' success')
except IOError:
io_error_list.append(code)
print(code+' IOError')
except ValueError:
value_error_list.append(code)
print(code+' ValueError')
except AttributeError:
attribute_error_list.append(code)
print(code+' AttributeError')
# In[ ]:
print(io_error_list)
print(value_error_list)
print(attribute_error_list)
error_dict = {'IOError':io_error_list, 'ValueError':value_error_list, 'AttributeError': attribute_error_list}
pickle.dump(error_dict, open('error_dict.pickle', 'wb'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment