python获取资产并写入mysql_python拉取股票数据存入mysql

# encoding:utf-8

import tushare as ts

import pandas as pd

import pymysql

import time

import datetime

import os

#节假日,跳过

rest = ["20180924","20181001","20181002","20181003","20181004","20181005","20181231","20190101","20190204","20190205","20190206","20190207",

"20190208"]

for k, v in enumerate(rest):

print( k, v)

config = {

'host':'106.14.145.51',

'port':4000,

'user':'pic98',

'password':'hello',

'database':'Stocks',

}

ts.set_token('你的挖地兔token')

#[挖地兔token获取地址:](https://tushare.pro/register?reg=129295)

pro = ts.pro_api()

#df = ts.get_today_ticks('600309')

#df.head(10)

#df = ts.get_tick_data('000001','2018-10-11',3,0,'tt')

df = pro.stock_basic()

print('-------')

db = pymysql.connect(**config)

cursor = db.cursor()

idx = 0

for dd in df.values:

idx = idx+1

if idx % 50 == 0:

os.system("cls")

try:

today=datetime.date.today()

oneday=datetime.timedelta(days=1)

li=[]

for i in range(0,5):

try:

today=today-oneday

ticktoday=datetime.datetime.strftime(today,'%Y-%m-%d')

ticktodayid=datetime.datetime.strftime(today,'%Y%m%d')

weekd = today.weekday()

if weekd == 5 or weekd == 6:

print(datetime.date.today(),ticktodayid,dd[0],"已有跳过(双休日)...")

continue

continue0_ = False

for k, v in enumerate(rest):

if v == ticktodayid:

continue0_ = True

break

if continue0_ == True:

print(ticktodayid,dd[0],"已有跳过(节假日)...")

continue

#print(ticktoday)

sql = "SELECT dayid,ts_code,value FROM PowerByDay WHERE ts_code = '%s' and dayid='%s' order by dayid desc"

data = (dd[0],ticktodayid)

cursor.execute(sql % data)

if cursor.rowcount > 0:

result = cursor.fetchall()

continue_ = False

for item in result:

if item[0] == int(ticktodayid):

continue_ = True

break

if continue_ == True:

print(ticktodayid,dd[0],"已有跳过(已入库)...")

continue

time.sleep(2)

tick = ts.get_tick_data(dd[1],ticktoday,3,0,'tt')

#print(tick)

if tick is None :

print(i,ticktodayid,dd[0],"无数据!")

continue

powertotal = 0

for tick0 in tick.values:

if tick0[5] == '卖盘' :

powertotal -= tick0[3]

elif tick0[5] == '买盘':

powertotal += tick0[3]

sql = 'INSERT INTO PowerByDay(DayId,ts_code, symbol, name, market,value ) VALUES (%s, "%s", "%s", "%s","%s",%.2f)'

data = (ticktodayid,dd[0],dd[1],dd[2],dd[5],powertotal)

print('%d\t%d\t%s\t%s\t%s\ttickcount:%d' % (idx,i,ticktodayid,dd[0],dd[2],tick.values.size))

print(datetime.date.today(),sql % data)

cursor.execute(sql % data)

db.commit()

except Exception as e:

print(e)

db.rollback()

except Exception as e:

print(e)

# 关闭数据库连接

db.close()

print('-------')

#df = pro.query('suspend', ts_code='', suspend_date='20180927', resume_date='', fiedls='')

#df = pro.suspend(ts_code='600848.SH', suspend_date='', resume_date='', fiedls='')

#print(df)

print(ts.__version__)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值