利用tushare获取基金数据存入mysql

最近做了一个基金网站,利用tuhsare获取ETF基金数据,然后存入本地mysql。

先建立获取基金的代码

import numpy as np
import pandas as pd
#from keras.optimizers import Adam
import tushare as ts
pro = ts.pro_api('e6cde7f6165daeaa49d539e2de4b4725944d64c70e1685e563eaebe8')
import time
import glob,os
import pymysql
def get_funds_data(code,start_date,end_date):    
    df = pro.fund_daily(**{
        "trade_date": "",
        "start_date":start_date,
        "end_date": end_date,
        "ts_code": code,
        "limit": "",
        "offset": ""
    }, fields=[
        "ts_code",
        "trade_date",
        "pre_close",
        "open",
        "high",
        "low",
        "close",
        "change",
        "pct_chg",
        "vol",
        "amount"
    ])
    return df

在写一个存入MySQL的代码

for i in range(len(name)):
    df=get_funds_data(name[i],start_date=day1,end_date=day1)
    df= np.array(df)
    time.sleep(0.3)
    #将更新基金数据写入数据库
    if(len(df)==0):#如果当天没有数据就跳过
        continue;
    print(tablename[i])
    sql = "INSERT INTO "+tablename[i]+"(ts_code,trade_date,pre_close,open,high,low,close,ts_change,pct_chg,vol,amount) \
           VALUES ('%s','%s','%s' ,'%s' ,'%s' ,'%s' ,'%s' ,'%s' ,'%s' ,'%s' ,'%s' )" % \
           (df[0][0],day,df[0][2],df[0][3],df[0][4],df[0][5],df[0][6],df[0][7],df[0][8],df[0][9],df[0][10])

    try:
       # Execute the SQL command
        cursor.execute(sql)
       # Commit your changes in the database
        db.commit()
    except:
       # Rollback in case there is any error
       db.rollback()

# disconnect from server
db.close()

其中调用获取基金的函数,然后将其存入表中

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据技术分析初学者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值