首先通過 Anaconda 安裝攜帶安裝好 python 安裝見官網,https://docs.anaconda.com/anaconda/install/windows/
如果XX module沒有,請命令行 pip3 install XX。 但是如果是這個 ModuleNotFoundError: No module named 'MySQLdb' 請用如下執行SQL。 import pymysql pymysql.install_as_MySQLdb() 廢話不多,簡單好用的tushare平臺,就是能夠用簡單的代碼獲取最多數據。 其中token獲取需要登錄 https://tushare.pro/document/1?doc_id=38 然後獲取token,https://tushare.pro/document/1?doc_id=39 案例就如下文,數據共享,小夥伴們直接用(記得數據庫連接修改下)。有問題留言或者 郵箱 [email protected]`
-
#coding=utf-8
-
import pandas as pd
-
import tushare as ts
-
from sqlalchemy import create_engine
-
import pymysql
-
pymysql.install_as_MySQLdb()
-
ts.set_token('ea3263c5XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
-
pro = ts.pro_api()
-
pro = ts.pro_api('ea3263c54XXXXXXXXXXXXXXXXXXXXXXXXXXX')
-
#函數列表
-
df_stock_basic = pro.stock_basic(fields='exchange,ts_code,symbol,name,area,industry,fullname,market,curr_type,list_status,list_date,delist_date,is_hs')
-
df_trade_cal = pro.trade_cal(field='exchange,cal_date,is_open,pretrade_date')
-
df_namechange = pro.namechange(fields='ts_code,name,start_date,end_date,change_reason')
-
df_hs_const = pro.hs_const(hs_type='SH', filed='ts_code,hs_type,in_date,out_date,is_new')
-
df_hs_const2 = pro.hs_const(hs_type='SZ', filed='ts_code,hs_type,in_date,out_date,is_new')
-
df_stock_company = pro.stock_company(field='ts_code,exchange,chairman,manager,secretary,reg_capital,setup_date,province,city,introduction,website,email,office,employees,main_business,business_scope')
-
df_new_share = pro.new_share(filed='ts_code,sub_code,name,ipo_date,issue_date,amount,market_amount,price,pe,limit_amount,funds,ballot')
-
df_bak_basic = pro.bak_basic(trade_date='', ts_code='',filed='trade_date,ts_code,name,industry,area,pe,float_share,total_share,total_assets,liquid_assets,fixed_assets,reserved,reserved_pershare,eps,bvps,pb,list_date,undp,per_undp,rev_yoy,profit_yoy,gpr,npr,holder_num')
-
engine_ts = create_engine('mysql://root:[email protected]:3306/tushare?charset=utf8&use_unicode=1')
-
#獲取股票列表
-
def write_data_stock_basic(df_stock_basic):
-
print(df_stock_basic)
-
print(df_stock_basic.shape)
-
res = df_stock_basic.to_sql('stock_basic', engine_ts, index=False, if_exists='append', chunksize=5000)
-
#交易日曆
-
def write_data_trade_cal(df_trade_cal):
-
res = df_trade_cal.to_sql('trade_cal', engine_ts, index=False, if_exists='append', chunksize=5000)
-
#股票曾用名
-
def write_data_namechange(df_namechange):
-
print(df_namechange)
-
print(df_namechange.shape)
-
res = df_trade_cal.to_sql('namechange', engine_ts, index=False, if_exists='append', chunksize=5000)
-
#滬深股通成份股
-
def write_data_hs_const(df_hs_const):
-
print(df_hs_const)
-
print(df_hs_const.shape)
-
res = df_hs_const.to_sql('hs_const', engine_ts, index=False, if_exists='append', chunksize=5000)
-
#滬深股通成份股
-
def write_data_hs_const2(df_hs_const2):
-
print(df_hs_const2)
-
print(df_hs_const2.shape)
-
res = df_hs_const2.to_sql('hs_const', engine_ts, index=False, if_exists='append', chunksize=5000)
-
#上市公司基本信息 單次提取4500條
-
def write_data_stock_company(df_stock_company):
-
print(df_stock_company)
-
print(df_stock_company.shape)
-
res = df_stock_company.to_sql('stock_company', engine_ts, index=False, if_exists='append', chunksize=5000)
-
#上市公司管理層
-
#管理層薪酬和持股
-
#IPO新股列表
-
def write_data_new_share(df_new_share):
-
print(df_new_share)
-
print(df_new_share.shape)
-
res = df_new_share.to_sql('new_share', engine_ts, index=False, if_exists='append', chunksize=5000)
-
#備用列表 單次 5000
-
def write_data_bak_basic(df_bak_basic):
-
print(df_bak_basic)
-
print(df_bak_basic.shape)
-
res = df_bak_basic.to_sql('bak_basic', engine_ts, index=False, if_exists='append', chunksize=5000)
-
if name == 'main':
-
print(df_trade_cal)
-
print(df_trade_cal.shape)
-
write_data_stock_basic(df_stock_basic)
-
write_data_trade_cal(df_trade_cal)
-
write_data_namechange(df_namechange)
-
write_data_hs_const(df_hs_const)
-
write_data_hs_const(df_hs_const2)
-
write_data_stock_company(df_stock_company)
-
write_data_bak_basic(df_bak_basic)`