def update_static_info(force=False): now = datetime.datetime.now() t = None rf = open(StaticInfo.storage_update_date, "r") line = rf.read() rf.close() wf = open(StaticInfo.storage_update_date, "w") if line: t = parser.parse(line) else: wf.write(str(now)) delta_day = (now - t).days if delta_day > 0 or force: wf.write(str(now)) print "static info need to update" ts.get_industry_classified().to_csv( StaticInfo.data_type["industry"], index=False) ts.get_concept_classified().to_csv(StaticInfo.data_type["concept"], index=False) ts.get_area_classified().to_csv(StaticInfo.data_type["area"], index=False) InitStaticInfo._get_stock_hist_data( StaticInfo.get_stock_pool().keys()) wf.close()
def 下载板块数据(self): # 获取不同分类数据 ts.get_industry_classified().to_csv('./股票数据/基本信息/行业分类.csv') ts.get_concept_classified().to_csv('./股票数据/基本信息/概念分类.csv') ts.get_area_classified().to_csv('./股票数据/基本信息/地域分类.csv') ts.get_sme_classified().to_csv('./股票数据/基本信息/中小板.csv') ts.get_gem_classified().to_csv('./股票数据/基本信息/创业板.csv') ts.get_st_classified().to_csv('./股票数据/基本信息/风险警示板.csv') ts.get_hs300s().to_csv('./股票数据/基本信息/沪深300.csv') ts.get_sz50s().to_csv('./股票数据/基本信息/上证50.csv') ts.get_zz500s().to_csv('./股票数据/基本信息/中证500.csv')
def regionData(self): table = 'stock_etl.stock_region' column = 'ticker,tickerName,tickerType' result = ts.get_area_classified() sql = SqlBuildUtil.SqlBuildUtil.insertBuildts(table, column, result.values) EtlDao.EtlDao().delAllDate(table) EtlDao.EtlDao().save(sql)
def save_area_classified(): ''' 获取地域分类信息 :return: ''' cur = mysql_connect.cursor() try: sta = cur.execute("delete from py_db.gp_area_classified") print(sta) print("删除地域分类gp_area_classified:", sta, "条数据完成") except Exception as e: print(e) mysql_connect.commit() cur.close() df = tushare.get_area_classified() try: pd.io.sql.to_sql(df, 'gp_area_classified', yconnect, schema='py_db', if_exists='append', index=df['code']) print("地域分类", len(df), "更新完成") return len(df) except ValueError as e: print(e) return False return -1
def fetch_classification(self): # 数据来源自新浪财经的行业分类/概念分类/地域分类 print( "Trying: get_today_all" ) today_all = ts.get_today_all() #一次性获取今日全部股价 set_today_all = set(today_all.T.values[0]) print( "Trying: get_industry_classified" ) industry_classified = ts.get_industry_classified() set_industry_classified = set(industry_classified.T.values[0]) print( "Trying: get_area_classified" ) area_classified = ts.get_area_classified() set_area_classified = set(area_classified.T.values[0]) print( "Trying: get_concept_classified" ) concept_classified = ts.get_concept_classified() set_concept_classified = set(concept_classified.T.values[0]) print( "Trying: get_sme_classified" ) sme_classified = ts.get_sme_classified() set_sme_classified = set(sme_classified.T.values[0]) return [ today_all , set_today_all , industry_classified , set_industry_classified , area_classified , set_area_classified , concept_classified , set_concept_classified , sme_classified , set_sme_classified ]
def get_arealist_info(): # 地域分类 df = ts.get_area_classified() res = df.to_sql(all_area_table, engine, if_exists='replace') msg = 'ok' if res is None else res print('获取地域分类: ' + msg + '\n') print(msg)
def classify_info_to_sql(): create_classify_table() a = ts.get_industry_classified() a.columns = ['code', 'name', 'industry'] b = ts.get_area_classified() c = ts.get_sz50s() c = c.iloc[:,1::] c['sz50'] = '1' d = ts.get_hs300s() d = d.iloc[:,1::] d.columns = ['code','name','hs300_weight'] e = ts.get_zz500s() e = e.iloc[:,1::] e.columns = ['code','name','zz500_weight'] result = pd.merge(a, b, how='left', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=('_x', '_y'), copy=True, indicator=False) result = pd.merge(result, c, how='left', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=('_x', '_y'), copy=True, indicator=False) result = pd.merge(result, d, how='left', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=('_x', '_y'), copy=True, indicator=False) result = pd.merge(result, e, how='left', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=('_x', '_y'), copy=True, indicator=False) df_to_mysql('anack_classify',result)
def get_area_classified(): ''' Return the area classified DataFrame :return: the DataFrame ''' content = ts.get_area_classified() return content
def fetch_classification(self): # 数据来源自新浪财经的行业分类/概念分类/地域分类 print("Trying: get_today_all") today_all = ts.get_today_all() #一次性获取今日全部股价 set_today_all = set(today_all.T.values[0]) print("Trying: get_industry_classified") industry_classified = ts.get_industry_classified() set_industry_classified = set(industry_classified.T.values[0]) print("Trying: get_area_classified") area_classified = ts.get_area_classified() set_area_classified = set(area_classified.T.values[0]) print("Trying: get_concept_classified") concept_classified = ts.get_concept_classified() set_concept_classified = set(concept_classified.T.values[0]) print("Trying: get_sme_classified") sme_classified = ts.get_sme_classified() set_sme_classified = set(sme_classified.T.values[0]) return [ today_all, set_today_all, industry_classified, set_industry_classified, area_classified, set_area_classified, concept_classified, set_concept_classified, sme_classified, set_sme_classified ]
def get_stock_basic() -> list: nodes, links = [], [] Node = namedtuple("Node", "name type source") Link = namedtuple("Link", "head link tail source") df = ts.get_stock_basics() df.reset_index(inplace=True) data = df.to_dict(orient="records") industry_mapper = ts.get_industry_classified().groupby( ["code"])["c_name"].apply(lambda x: x.tolist()).to_dict() concept_mapper = ts.get_concept_classified().groupby( ["code"])["c_name"].apply(lambda x: x.tolist()).to_dict() area_mapper = ts.get_area_classified().groupby( ["code"])["area"].apply(lambda x: x.tolist()).to_dict() for d in data: nodes.append(Node(d["code"], "公司", "tu")) nodes.append(Node(d["name"], "公司", "tu")) links.append(Link(d["code"], "等于", d["name"], "tu")) for k in set(industry_mapper.get(d["code"], []) + [d["industry"]]): nodes.append(Node(k, "行业", "tu")) links.append(Link(d["code"], "属于", k, "tu")) for k in concept_mapper.get(d["code"], []): nodes.append(Node(k, "概念", "tu")) links.append(Link(d["code"], "属于", k, "tu")) for k in set(area_mapper.get(d["code"], []) + [d["area"]]): nodes.append(Node(k, "区域", "tu")) links.append(Link(d["code"], "属于", k, "tu")) del d["industry"], d["area"] nodes = list(set(nodes)) return nodes, links, data
def update_stock_basic() -> None: df = ts.get_stock_basics() df.reset_index(inplace=True) data = df.to_dict(orient="records") # update industry industry_df = ts.get_industry_classified() industry_df = industry_df.groupby(["code"])["c_name"].apply(lambda x: x.tolist()).reset_index() industry_mapper = dict([(d["code"], d["c_name"]) for d in industry_df.to_dict(orient="records")]) # update concept concept_df = ts.get_concept_classified() concept_df = concept_df.groupby(["code"])["c_name"].apply(lambda x: x.tolist()).reset_index() concept_mapper = dict([(d["code"], d["c_name"]) for d in concept_df.to_dict(orient="records")]) # update area area_df = ts.get_area_classified() area_df = area_df.groupby(["code"])["area"].apply(lambda x: x.tolist()).reset_index() area_mapper = dict([(d["code"], d["area"]) for d in area_df.to_dict(orient="records")]) bar = ProgressBar(total=len(data)) for d in data: d["industry"] = list(set(industry_mapper.get(d["code"], []) + [d["industry"]])) d["concept"] = concept_mapper.get(d["code"], []) d["area"] = list(set(area_mapper.get(d["code"], []) + [d["area"]])) bar.move() db.stock_basics.update({"code": d["code"]}, {"$set": d}, True) bar.log("code: {} name: {} industry {} concept {} area {}" .format(d["code"], d["name"], d["industry"], d["concept"], d["area"]))
def fetch_stock_area(): ''' 获取股票 地区数据 :return: ''' df = ts.get_area_classified() return df
def classification(class_types): if class_types == 'industry': industry_classified = ts.get_industry_classified() industry_classified.to_csv('D:\\ts\\classification\\industry_classified.csv', encoding='gbk') elif class_types == 'concept': concept_classified = ts.get_concept_classified() concept_classified.to_csv('D:\\ts\\classification\\concept_classified.csv', encoding='gbk') elif class_types == 'area': area_classified = ts.get_area_classified() area_classified.to_csv('D:\\ts\\classification\\area_classified.csv', encoding='gbk') elif class_types == 'sme': sme_classified = ts.get_sme_classified() sme_classified.to_csv('D:\\ts\\classification\\sme_classified.csv', encoding='gbk') elif class_types == 'gem': gem_classified = ts.get_gem_classified() gem_classified.to_csv('D:\\ts\\classification\\gem_classified.csv', encoding='gbk') elif class_types == 'st': st_classified = ts.get_st_classified() st_classified.to_csv('D:\\ts\\classification\\st_classified.csv', encoding='gbk') elif class_types == 'hs300': hs300s = ts.get_hs300s() hs300s.to_csv('D:\\ts\\classification\\hs300s.csv', encoding='gbk') elif class_types == 'sz50': sz50s = ts.get_sz50s() sz50s.to_csv('D:\\ts\\classification\\sz50s.csv', encoding='gbk') elif class_types == 'zz500': zz500s = ts.get_zz500s() zz500s.to_csv('D:\\ts\\classification\\zz500s.csv', encoding='gbk') elif class_types == 'terminated': terminated = ts.get_terminated() terminated.to_csv('D:\\ts\\classification\\terminated.csv', encoding='gbk') elif class_types == 'suspended': suspended = ts.get_suspended() suspended.to_csv('D:\\ts\\classification\\suspended.csv', encoding='gbk')
def core_function(self, func): self.set_data() mongo = MongoClient("127.0.0.1", 27017) if (func == "industry_classified"): df = ts.get_industry_classified() elif (func == "concept_classified"): df = ts.get_concept_classified() elif (func == "area_classified"): df = ts.get_area_classified() elif (func == "gem_classified"): df = ts.get_gem_classified() elif (func == "sme_classified"): df = ts.get_sme_classified() elif (func == "st_classified"): df = ts.get_st_classified() elif (func == "hs300s"): df = ts.get_hs300s() elif (func == "sz50s"): df = ts.get_sz50s() elif (func == "zz500s"): df = ts.get_zz500s() print(df) elif (func == "terminated"): df = ts.get_terminated() else: df = {} insert_string = df.to_json(orient='records') items = json.loads(insert_string) coll = mongo.classify[func] coll.insert(items)
def get_area_classified(): try: df = ts.get_area_classified(); engine = create_engine('mysql://*****:*****@127.0.0.1/stock?charset=utf8') # df.insert(0,'code','600848') df.to_sql('area_classified', engine, if_exists='append') except Exception, e: e.message
def get_area_classified(): try: logger.info('get area data starting...') df = ts.get_area_classified() logger.info('get area data end...') except: logger.exception('some errors between get area data end...') return df
def get_area_classified(self): """ 地域分类 """ area = ts.get_area_classified() area = area[['code','area']] area.columns = ['code','areaName'] self.data = pd.merge(self.data,area,on=['code'],how='outer')
def ini_classfication(): Industry = ts.get_industry_classified() Concept = ts.get_concept_classified() Area = ts.get_area_classified() _Codelist = Area[['code']] Codelist = [] for i in range(len(_Codelist)): Codelist.append(_Codelist.ix[i, 0]) return [Codelist, Area, Concept, Industry]
def get_area_classification(): filename = 'area_classification' filepath = os.path.join(rootDir, 'StockBasis', filename + '.csv') if (os.path.exists(filepath)): data = pd.read_csv(filepath, encoding='gbk') else: data = ts.get_area_classified() data.to_csv(filepath) return data
def area_classified(request): try: data = ts.get_area_classified() res = { 'columns': ['股票代码', '股票名称', '地域名称'], 'data': json.loads(json.dumps(data.values, cls=DateEncoder)) } except (BaseException): return HttpResponse(BaseException) else: return HttpResponse(json.dumps(res), content_type="application/json")
def getAreaClassified(): ''' 获取地域分类。按地域对股票进行分类,即查找出哪些股票属于哪个省份。 参数说明: file_path:文件路径,默认为None即由TuShare提供,可以设定自己的股票文件路径。 返回值说明: code:股票代码 name:股票名称 area:地域名称 ''' return ts.get_area_classified()
def getstockclassifieddata(): mylogger = getmylogger() print("start") scdict = { 'industry': '行业分类', 'concept': '概念分类', 'area': '地域分类', 'sme': '中小板分类', 'gem': '创业板分类', 'st': '风险警示板分类', 'hs300s': '沪深300成份及权重', 'sz50s': '上证50成份股', 'zz500s': '中证500成份股', 'terminated': '终止上市股票列表', 'suspended': '暂停上市股票列表' } for sc in scdict: sctbname = sc scinfo = scdict[sctbname] try: if sc == "industry": df = ts.get_industry_classified() elif sc == "concept": df = ts.get_concept_classified() elif sc == "area": df = ts.get_area_classified() elif sc == "sme": df = ts.get_sme_classified() elif sc == "gem": df = ts.get_gem_classified() elif sc == "st": df = ts.get_st_classified() elif sc == "hs300s": df = ts.get_hs300s() elif sc == "zz500s": df = ts.get_zz500s() elif sc == "sz50s": df = ts.get_sz50s() elif sc == "terminated": df = ts.get_terminated() elif sc == "suspended": df = ts.get_suspended() else: mylogger.info("没有执行命令。") if df is not None: tosql(df, sctbname, "replace", scinfo, mylogger) else: mylogger.info("没有%s数据。" % scinfo) except Exception: tracelog = traceback.format_exc() mylogger.info("获取数据异常。") mylogger.info(tracelog)
def load_data(): try: rs = ts.get_area_classified() pd.DataFrame.to_sql(rs, table_name, con=conn, flavor='mysql', if_exists='replace', index=True) except Exception as e: print(e.message) print("公司地域分类信息数据出错")
def classReport(): ''' 股票分类报告 ''' class_industry = ts.get_industry_classified() class_industry['code'] = class_industry['code'].astype(str) class_industry = class_industry.drop(['name'], axis=1) print " " class_area = ts.get_area_classified() class_area['code'] = class_area['code'].astype(str) class_area = class_area.drop(['name'], axis=1) return class_industry, class_area
def load_data(): try: rs = ts.get_area_classified() pd.DataFrame.to_sql(rs, name=table_name, con=con, schema=db, if_exists='replace', index=True) except Exception as e: print(e) print("公司地域分类信息数据出错")
def ts_to_csv(): stock_data = ts.get_stock_basics() # stock_data.to_csv('stock.csv',columns=['name']) stock_edges = pd.DataFrame(columns=['fromID', 'toID', 'type']) block_nodes = {} begin_code = int(900001); # industry block_industry = ts.get_industry_classified() for row in block_industry.iterrows(): if row[1]['c_name'] not in block_nodes: block_nodes[row[1]['c_name']] = begin_code begin_code += 1 stock_edges = stock_edges.append({'fromID': row[1]['code'], 'toID': str(block_nodes[row[1]['c_name']]), 'type': '属于'}, ignore_index=True) # block_industry.to_csv('block_industry.csv',index=None) # concept block_concept = ts.get_concept_classified() for row in block_concept.iterrows(): if row[1]['c_name'] not in block_nodes: block_nodes[row[1]['c_name']] = begin_code begin_code += 1 stock_edges = stock_edges.append({'fromID': row[1]['code'], 'toID': str(block_nodes[row[1]['c_name']]), 'type': '属于'}, ignore_index=True) # block_concept.to_csv('block_concept.csv',index=None) # area block_area =ts.get_area_classified() for row in block_area.iterrows(): if row[1]['area'] not in block_nodes: block_nodes[row[1]['area']] = begin_code begin_code += 1 stock_edges = stock_edges.append({'fromID': row[1]['code'], 'toID': str(block_nodes[row[1]['area']]), 'type': '属于'}, ignore_index=True) # block_area.to_csv('block_area.csv',index=None) # print(stock_edges) stock_edges.to_csv('stock_edges.csv',index=None, encoding="utf-8"); #to csv # 1. 创建文件对象 block_df = pd.DataFrame(columns=['code', 'name']) for k,v in block_nodes.items(): # print(k) block_df = block_df.append({'code': str(v), 'name': k}, ignore_index=True) block_df = block_df.set_index('code') block_df = block_df.append(stock_data) block_df.to_csv('stock_nodes.csv',columns=['name'], encoding="utf-8")
def main(): dbclient = SqliteClient(base=Base, url='sqlite:///./share.db') # get_report_data(2017, 3, engine) # rep = ts.cap_tops() print(datetime.now()) areas = ts.get_area_classified() print(datetime.now()) areas = tushareTable.pandaToORM(areas, areaClassified) print(datetime.now()) dbclient.save_all(areas) print(datetime.now()) return
def refresh_baseinfo_table(): print("Fetching area data (地域分类)... ", end='') df = ts.get_area_classified() sql = "INSERT INTO baseinfo(code, name, area) VALUES(?, ?, ?)" rss = np.array(df).tolist() db.conn().exem(sql, rss) print("is done! And saved data to table [baseinfo] success.") def update_baseinfo(df, field): sql = "UPDATE baseinfo SET " + field + "=1 WHERE code=?" # css = np.array(df[['code']]).tolist() css = list(df.code) db.conn().exem(sql, css) print("is done! And update table [baseinfo] success.")
def list(self, stock_block_type): stock_block = None if stock_block_type == self.industry: stock_block = db.get(STOCK_BLOCK_INDUSTRY) if stock_block is None: stock_block = ts.get_industry_classified() db.save(STOCK_BLOCK_INDUSTRY, stock_block) elif stock_block_type == self.concept: stock_block = db.get(STOCK_BLOCK_CONCEPT) if stock_block is None: stock_block = ts.get_concept_classified() db.save(STOCK_BLOCK_CONCEPT, stock_block) elif stock_block_type == self.area: stock_block = db.get(STOCK_BLOCK_AREA) if stock_block is None: stock_block = ts.get_area_classified() db.save(STOCK_BLOCK_AREA, stock_block) elif stock_block_type == self.sme: stock_block = db.get(STOCK_BLOCK_SME) if stock_block is None: stock_block = ts.get_sme_classified() db.save(STOCK_BLOCK_SME, stock_block) elif stock_block_type == self.gem: stock_block = db.get(STOCK_BLOCK_GEM) if stock_block is None: stock_block = ts.get_gem_classified() db.save(STOCK_BLOCK_GEM, stock_block) elif stock_block_type == self.st: stock_block = db.get(STOCK_BLOCK_ST) if stock_block is None: stock_block = ts.get_st_classified() db.save(STOCK_BLOCK_ST, stock_block) elif stock_block_type == self.hs300s: stock_block = db.get(STOCK_BLOCK_HS300S) if stock_block is None: stock_block = ts.get_hs300s() db.save(STOCK_BLOCK_HS300S, stock_block) elif stock_block_type == self.sz50s: stock_block = db.get(STOCK_BLOCK_SZ50S) if stock_block is None: stock_block = ts.get_sz50s() db.save(STOCK_BLOCK_SZ50S, stock_block) elif stock_block_type == self.zz500s: stock_block = db.get(STOCK_BLOCK_ZZ500S) if stock_block is None: stock_block = ts.get_zz500s() db.save(STOCK_BLOCK_ZZ500S, stock_block) else: return None return stock_block
def update_area(request): '''更新地域数据''' stock_area.objects.all().delete() rltobj = ts.get_area_classified() objlist = [] for i in range(len(rltobj)): tmpobj = stock_area(code=rltobj.loc[i]['code'], name=rltobj.loc[i]['name'], a_name=rltobj.loc[i]['area']) objlist.append(tmpobj) stock_area.objects.bulk_create(objlist) return HttpResponse('succ.{0}条!'.format(len(objlist)))
def save_area_classified(): """按地域对股票进行分类,即查找出哪些股票属于哪个省份。""" logger.info('Begin get and save area clssified.') try: data_df = ts.get_area_classified() if data_df is not None and not data_df.empty: data = data_df.values data_dicts = [{ 'code': row[0], 'name': row[1], 'area': row[2] } for row in data] AreaClassified.insert_many(data_dicts).execute() logger.info('Success get and save area clssified.') else: logger.warn('Empty get and save area clssified.') except Exception as e: logger.exception('Error get and save area clssified.')
def preload(): stock_block = ts.get_industry_classified() db.save(STOCK_BLOCK_INDUSTRY, stock_block) stock_block = ts.get_concept_classified() db.save(STOCK_BLOCK_CONCEPT, stock_block) stock_block = ts.get_area_classified() db.save(STOCK_BLOCK_AREA, stock_block) stock_block = ts.get_sme_classified() db.save(STOCK_BLOCK_SME, stock_block) stock_block = ts.get_gem_classified() db.save(STOCK_BLOCK_GEM, stock_block) stock_block = ts.get_st_classified() db.save(STOCK_BLOCK_ST, stock_block) stock_block = ts.get_hs300s() db.save(STOCK_BLOCK_HS300S, stock_block) stock_block = ts.get_sz50s() db.save(STOCK_BLOCK_SZ50S, stock_block) stock_block = ts.get_zz500s() db.save(STOCK_BLOCK_ZZ500S, stock_block)
def getStockClassfied(): filepath = 'f:\\stockdata\\股票分类数据\\' index = 0 # 行业分类 df = ts.get_industry_classified() saveCsv(df, filepath, stockClassList, index) index += 1 # 概念分类 df = ts.get_concept_classified() saveCsv(df, filepath, stockClassList, index) index += 1 # 地域分类 df = ts.get_area_classified() saveCsv(df, filepath, stockClassList, index) index += 1 # 中小板分类 df = ts.get_sme_classified() saveCsv(df, filepath, stockClassList, index) index += 1 # 创业板分类 df = ts.get_gem_classified() saveCsv(df, filepath, stockClassList, index) index += 1 # 风险警示板分类 df = ts.get_st_classified() saveCsv(df, filepath, stockClassList, index) index += 1 # 沪深300成份及权重 df = ts.get_hs300s() saveCsv(df, filepath, stockClassList, index) index += 1 # 上证50成份股 df = ts.get_sz50s() saveCsv(df, filepath, stockClassList, index) index += 1 # 中证500成份股 df = ts.get_zz500s() saveCsv(df, filepath, stockClassList, index) index += 1
def get_area_classified(force=False): cache_file = cache_path + 'area_class_{date}.csv'.format( date=datetime.now().strftime('%Y%m%d')) if force is False and os.path.exists(cache_file): return pd.read_csv(cache_file, dtype={'code': str}, index_col=0) area_cls_df = ts.get_area_classified() area_names = area_cls_df['area'].unique() area_codes = range(1, 1 + len(area_cls_df['area'].unique())) area = pd.DataFrame(np.full((len(area_names), 2), np.nan), columns=['area', 'area_code']) area['area'] = area_names area['area_code'] = area_codes area_cls_df = pd.merge(area_cls_df, area, on='area') area_cls_df.to_csv(cache_file) return area_cls_df
def get_area(): ''' 函数功能: -------- 获取沪深上市公司所属的地域信息。 输入参数: -------- 无 输出参数: -------- DataFrame code,代码 name,名称 area,所属地域 ''' # Download data area = ts.get_area_classified() # Return Dataframe return area
def get_area(self, year): i = ts.get_area_classified()[['code', 'area']] j = pd.read_csv('profit_data_%d.csv'%year, dtype={'code':str})[['code', 'roe', 'net_profit_ratio']] j = j[(j.net_profit_ratio>-100)&(j.net_profit_ratio<100)&(j.roe>-100)&(j.roe<100)] data = pd.merge(i, j, on='code').groupby(by='area').mean() return data
#!/usr/bin/python from sqlalchemy import create_engine import tushare as ts import os #result=ts.get_industry_classified() engine=create_engine('mysql://*****:*****@127.0.0.1/stock?charset=utf8',echo=True) #result.to_sql('industry',engine) result=ts.get_area_classified() result.to_sql('area',engine) #result.to_excel('/Users/xzp/industry.xlsx')
def typearea(): global dftypearea dftypearea = ts.get_area_classified() dftypearea.insert(0,'uploadtime',nowtime) typearea_tosql()
def getIndustry(): df = ts.get_area_classified() df.to_csv('area.csv', encoding='gbk') print(df)
def load_company_industry_info(): #下载加载行业分类数据 try: rs=ts.get_industry_classified() sql.write_frame(rs, "company_industry_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载公司行业分类信息ok") except: print("下载公司行业分类信息出错") #下载加载概念分类数据 try: rs=ts.get_concept_classified() sql.write_frame(rs, "company_concept_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("载公司概念分类信息ok") except: print("下载公司概念分类信息出错") #下载加载地域分类数据 try: rs=ts.get_area_classified() sql.write_frame(rs, "company_area_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载公司区域分类信息ok") except: print("下载公司区域分类信息出错") #下载加载中小板分类数据 try: rs=ts.get_sme_classified() sql.write_frame(rs, "company_sme_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载中小板分类数据ok") except: print("下载中小板分类数据出错") #下载加载创业板分类数据 try: rs=ts.get_gem_classified() sql.write_frame(rs, "company_gem_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载创业板分类数据ok") except: print("下载创业板分类数据出错") #下载加载st板分类数据 try: rs=ts.get_st_classified() sql.write_frame(rs, "company_st_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载st板分类数据ok") except: print("下载st板分类数据出错") #下载加载沪深300板分类数据 try: rs=ts.get_hs300s() sql.write_frame(rs, "company_hs300_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载加载沪深300板分类数据ok") except: print("下载加载沪深300板分类数据出错") #下载加载上证50板分类数据 try: rs=ts.get_sz50s() sql.write_frame(rs, "company_sz50_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载加载上证50板分类数据ok") except: print("下载加载上证50板分类数据出错") #下载加载中证500板分类数据 try: rs=ts.get_zz500s() sql.write_frame(rs, "company_zz500_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载加载中证500板分类数据ok") except: print("下载加载中证500板分类数据出错") #下载加载终止上市分类数据 try: rs=ts.get_terminated() sql.write_frame(rs, "company_terminated_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载加载终止上市分类数据ok") except: print("下载加载终止上市分类数据出错") #下载加载暂停上市分类数据 try: rs=ts.get_suspended() sql.write_frame(rs, "company_suspended_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载加载暂停上市分类数据ok") except: print("下载加载暂停上市分类数据出错")
# -*- coding: utf-8 -*- """ Created on Wed Aug 31 14:41:27 2016 @author: colec """ import tushare as ts import csv file_csv = open('AreaStatistic.csv', 'wb+') spamwriter = csv.writer(file_csv,dialect='excel') spamwriter.writerow(['code', 'name', 'area']) all_stock = ts.get_area_classified() for code, stockRow in all_stock.iterrows(): areaName = str(stockRow["area"]) if (areaName.find("天津") != -1): spamwriter.writerow([code, stockRow["name"].decode('utf-8').encode('gb2312'), stockRow["area"].decode('utf-8').encode('gb2312')]) file_csv.close()
def get_location(self): df = ts.get_area_classified() print df # df.to_excel('location.xls') self.save_to_excel(df,'location.xls')
#coding=utf-8 ''' Created on 2015年6月4日 @author: Administrator ''' import tushare as ts # 行业分类 ts.get_industry_classified() # 概念分类 ts.get_concept_classified() # 地域分类 ts.get_area_classified() # 获取沪深300当前成份股及所占权重 ts.get_hs300s() # 获取中小板股票数据,即查找所有002开头的股票 ts.get_sme_classified() # 获取创业板股票数据,即查找所有300开头的股票 ts.get_gem_classified() # 获取风险警示板股票数据,即查找所有st股票 ts.get_st_classified() # 获取上证50成份股 ts.get_sz50s()
#!/usr/bin/env python #coding=utf-8 import tushare as ts import pandas as pd import time # 行业分类 inds = ts.get_industry_classified() inds.columns = ('code', 'name', 'industry') time.sleep(1) # 概念分类 concepts = ts.get_concept_classified() concepts.columns = ('code', 'name', 'concept') time.sleep(1) # 地域分类 areas = ts.get_area_classified() areas.columns = ('code', 'name', 'area') time.sleep(1) # 中小板块 smalls = ts.get_sme_classified() time.sleep(1) # 创业版 news = ts.get_gem_classified() time.sleep(1) # st版块 sts = ts.get_st_classified() time.sleep(1)