tushare專題-金融股票數據獲取正確姿勢

首先通過 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]`

  1. #coding=utf-8

  2. import pandas as pd

  3. import tushare as ts

  4. from sqlalchemy import create_engine

  5. import pymysql

  6. pymysql.install_as_MySQLdb()

  7. ts.set_token('ea3263c5XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')

  8. pro = ts.pro_api()

  9. pro = ts.pro_api('ea3263c54XXXXXXXXXXXXXXXXXXXXXXXXXXX')

  10. #函數列表

  11. 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')

  12. df_trade_cal = pro.trade_cal(field='exchange,cal_date,is_open,pretrade_date')

  13. df_namechange = pro.namechange(fields='ts_code,name,start_date,end_date,change_reason')

  14. df_hs_const = pro.hs_const(hs_type='SH', filed='ts_code,hs_type,in_date,out_date,is_new')

  15. df_hs_const2 = pro.hs_const(hs_type='SZ', filed='ts_code,hs_type,in_date,out_date,is_new')

  16. 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')

  17. 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')

  18. 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')

  19. engine_ts = create_engine('mysql://root:[email protected]:3306/tushare?charset=utf8&use_unicode=1')

  20. #獲取股票列表

  21. def write_data_stock_basic(df_stock_basic):

  22. print(df_stock_basic)
    
  23. print(df_stock_basic.shape)
    
  24. res = df_stock_basic.to_sql('stock_basic', engine_ts, index=False, if_exists='append', chunksize=5000)
    
  25. #交易日曆

  26. def write_data_trade_cal(df_trade_cal):

  27. res = df_trade_cal.to_sql('trade_cal', engine_ts, index=False, if_exists='append', chunksize=5000)
    
  28. #股票曾用名

  29. def write_data_namechange(df_namechange):

  30. print(df_namechange)
    
  31. print(df_namechange.shape)
    
  32. res = df_trade_cal.to_sql('namechange', engine_ts, index=False, if_exists='append', chunksize=5000)

  33. #滬深股通成份股

  34. def write_data_hs_const(df_hs_const):

  35. print(df_hs_const)
    
  36. print(df_hs_const.shape)
    
  37. res = df_hs_const.to_sql('hs_const', engine_ts, index=False, if_exists='append', chunksize=5000)
    
  38. #滬深股通成份股

  39. def write_data_hs_const2(df_hs_const2):

  40. print(df_hs_const2)
    
  41. print(df_hs_const2.shape)
    
  42. res = df_hs_const2.to_sql('hs_const', engine_ts, index=False, if_exists='append', chunksize=5000)
    
  43. #上市公司基本信息 單次提取4500條

  44. def write_data_stock_company(df_stock_company):

  45. print(df_stock_company)
    
  46. print(df_stock_company.shape)
    
  47. res = df_stock_company.to_sql('stock_company', engine_ts, index=False, if_exists='append', chunksize=5000)

  48. #上市公司管理層

  49. #管理層薪酬和持股

  50. #IPO新股列表

  51. def write_data_new_share(df_new_share):

  52. print(df_new_share)
    
  53. print(df_new_share.shape)
    
  54. res = df_new_share.to_sql('new_share', engine_ts, index=False, if_exists='append', chunksize=5000)

  55. #備用列表 單次 5000

  56. def write_data_bak_basic(df_bak_basic):

  57. print(df_bak_basic)
    
  58. print(df_bak_basic.shape)
    
  59. res = df_bak_basic.to_sql('bak_basic', engine_ts, index=False, if_exists='append', chunksize=5000)

  60. if name == 'main':

  61. print(df_trade_cal)
    
  62. print(df_trade_cal.shape)
    
  63. write_data_stock_basic(df_stock_basic)
    
  64. write_data_trade_cal(df_trade_cal)
    
  65. write_data_namechange(df_namechange)
    
  66. write_data_hs_const(df_hs_const)
    
  67. write_data_hs_const(df_hs_const2)
    
  68. write_data_stock_company(df_stock_company)
    
  69. write_data_bak_basic(df_bak_basic)`
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章