从tushare网站获取股票基本信息并存入数据库中的python源代码

3 篇文章 0 订阅
3 篇文章 1 订阅

要对股票进行分析,需要关注股票的基本信息,例如上市时间、市值,所属行业等等,本文以从tushare网站获取相关信息为例,展示如何获取并存入到mysql数据库中,闲言少叙,直接上代码:

#coding:utf8
import tushare as ts
import pandas as pd
from sqlalchemy import create_engine
import pymysql
import configparser
import time

startTime=int(time.time())
# print('StartTime'+str(startTime))
# print(type(startTime))
conf = configparser.ConfigParser()
conf.read('config.ini')
userName = conf['db']['name']
password= conf['db']['password']
dbHost = conf['db']['host']
dbPort = conf['db']['port']
dbName = conf['db']['dbname']
tsToken = conf['tushare']['token']

dbConnect=create_engine('mysql+pymysql://'+userName+":"
    +password+"@"+dbHost+":"+dbPort+"/"+dbName+"?charset=utf8")
conn = pymysql.connect(host=dbHost,user=userName,
    password=password,database=dbName,charset='utf8')
cursor=conn.cursor()
ts.set_token(tsToken)

cursor.execute("drop table if exists basiclist")
cursor.execute("drop table if exists stock_company")
cursor.execute("drop table if exists tmp_basicInfo")
conn.commit()

pro=ts.pro_api()
#the basic information of stock
print("Getting the basic Information with stock_basic-------")
tmpSql='ts_code,symbol,name,area,industry,fullname,enname,market,' \
       'exchange,curr_type,list_status,list_date,delist_date,is_hs'
data1=pro.stock_basic(exchange='',list_status='L',fields=tmpSql)
data2=pro.stock_basic(exchange='',list_status='D',fields=tmpSql)
data=data1.append(data2)
data3=pro.stock_basic(exchange='',list_status='P',fields=tmpSql)
data=data.append(data3)
pd.io.sql.to_sql(data,'basiclist',dbConnect,schema='stocks', if_exists='replace',index=True)

print("Getting the company Information with stock_company-------")
#the basic information of stock company
tmpSql='ts_code,exchange,chairman,manager,secretary,reg_' \
       'capital,setup_date,province,city,introduction,website,' \
       'email,office,employees,main_business,business_scope'
df1 = pro.stock_company(exchange='SZSE',fields=tmpSql)
df2 = pro.stock_company(exchange='SSE',fields=tmpSql)
df=df2.append(df1)
pd.io.sql.to_sql(df,'stock_company',dbConnect,schema='stocks',
                 if_exists='replace',index=True)

print("drop table if exists tmp_basicInfo")
cursor.execute("drop table if exists tmp_basicInfo")
print("create tmp table and insert data")
tmpSql="create table tmp_basicInfo  select a.ts_code," \
       "a.symbol,a.name,a.area,a.industry,a.fullname,a.enname," \
       "a.market,a.curr_type,a.list_status,a.list_date," \
       "a.delist_date,a.is_hs,b.chairman,b.manager,b.secretary," \
       "b.reg_capital,b.setup_date,b.province,b.city,b.introduction," \
       "b.website,b.email,b.office,b.employees,b.main_business," \
       "b.business_scope from basiclist as a  left join stock_company" \
       " as b on a.ts_code =b.ts_code "
cursor.execute(tmpSql)
print("delete from basicInfo")
cursor.execute("delete from basicInfo")
print("insert the new data into basicInfo")
tmpSql="insert into basicInfo(ts_code,symbol,name,area,industry," \
       "fullname,enname,market,curr_type,list_status,list_date," \
       "delist_date,is_hs,chairman,manager,secretary,reg_capital," \
       "setup_date,province,city,introduction,website,email,office," \
       "employees,main_business,business_scope) select ts_code," \
       "symbol,name,area,industry,fullname,enname,market,curr_type," \
       "list_status,list_date,delist_date,is_hs,chairman,manager," \
       "secretary,reg_capital,setup_date,province,city,introduction," \
       "website,email,office,employees,main_business,business_scope " \
       " from tmp_basicInfo"
cursor.execute(tmpSql)
cursor.execute("update basicInfo set employees=0 where employees is NULL")
cursor.execute("update basicInfo set reg_capital=0 where reg_capital is NULL")
cursor.execute("drop table if exists basiclist")
cursor.execute("drop table if exists stock_company")
cursor.execute("drop table if exists tmp_basicInfo")
conn.commit()
endTime=int(time.time())
strSql="insert into crontablog(name,start,end,timelong) value('basicInfo',"+str(startTime)+","+str(endTime)+","+str(endTime-startTime)+")"
print(strSql)
cursor.execute(strSql)
conn.commit()
cursor.close()
conn.close()

-------------------- 正文到此结束------------------------

推荐一个公众号:健哥聊量化,会持续推出股票相关基础知识,以及python实现的一些基本的分析代码。欢迎大家关注,二维码如下:

å¥å¥èéå

相关文章列表如下:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值