def stock_zh_index_spot(): """ 描述: 股票指数数据是从新浪财经获取的数据 限量: 单次返回所有指数的实时行情数据 """ stock_df = ak.stock_zh_index_spot() return stock_df
def code(self): etf = ak.fund_em_etf_fund_daily() etf = etf[['基金代码', '基金简称', '类型', ]] etf.columns = ['code','name', 'market'] etf['market'] = 'ETF' etf['symbol'] = etf['code'].apply(lambda x:'sh' + str(x)[:6] if int(x) > 500000 else 'sz' + str(x)[:6]) etf['tag'] = etf['symbol'].apply(lambda x: 'E_' + str(x)) etf = etf[['name', 'code', 'symbol', 'tag', 'market']] index = ak.stock_zh_index_spot() index['code'] = index['symbol'].apply(lambda x:str(x)[2:]) index['tag'] = index['symbol'].apply(lambda x:'I_' + str(x)) index.loc[index['name']=='','name']='TBD' index['market'] = 'INDEX' index = index[['name','code','symbol', 'tag','market']] stock = ak.stock_zh_a_spot() stock['tag'] = stock['symbol'].apply(lambda x: 'S_' + str(x)) stock.to_csv(self.BASIC + 'stock_spot.csv', encoding='gb18030', index=False) stock['tag'] = stock['symbol'].apply(lambda x:'S_' + str(x)) stock['market'] = 'STOCK' stock = stock[['name','code','symbol', 'tag','market']] df = pd.concat([etf,index,stock],axis=0) df = df.append({'name':'银证转账', 'code':'817909','symbol':'sh817909','tag':'C_sh817909','market':'TRADE'},ignore_index=True) df = df.append({'name':'测试账号','code':'817910', 'symbol':'sh817910','tag':'C_sh817910','market':'TRADE'},ignore_index=True) df['name'] = df['name'].apply(lambda x: x[:-5] if x[-5:] == 'ETF行情' else x) df['name'] = df['name'].apply(lambda x: x[:-2] if x[-2:] == '行情' else x) df=df[df['name'].str.contains('ST')==False] df.to_csv(self.BASIC + 'code_name.csv', encoding='gb18030', index=False) return
def get_index_price(index_code): global stock_zh_index_spot_df if stock_zh_index_spot_df.empty: print("get index information") stock_zh_index_spot_df = ak.stock_zh_index_spot() return float( stock_zh_index_spot_df.set_index('code').at[index_code, 'trade'])
def get_index_price_25(self, code): # newest_data df = ak.stock_zh_index_spot() # print(df) newest_data = float( pd.to_numeric(df.loc[df[u'代码'] == code, '最新价']).astype('float')) # history_data history_data = ak.stock_zh_index_daily_em(symbol=code) #print(original_data) return (newest_data, history_data.iloc[-25, 2])
def get_stock_df(): stock_df = ak.stock_zh_index_spot() stock_df_filter = stock_df[(stock_df['代码'] == 'sh000001') | (stock_df['代码'] == 'sz399001') | (stock_df['代码'] == 'sz399006')] result = [] for index, row in stock_df_filter.iterrows(): result.append({ 'symbol': row["代码"], 'name': row["名称"], 'current_price': round(row["最新价"], 2), 'price_change': round(row["涨跌额"], 2), 'percent_change': round(row["涨跌幅"], 2), 'turnover': round((row["成交额"] / 100000000), 2) }) # return result resu = {'code': 200, 'data': result, 'message': '成功'} return json.dumps(resu, ensure_ascii=False)
#!/usr/local/bin/python3 # -*- coding: utf-8 -*- import akshare as ak import libs.common as common print(ak.__version__) #stock_sse_summary_df = ak.stock_sse_summary() #print(stock_sse_summary_df) # 接口: stock_zh_index_spot # 目标地址: http://vip.stock.finance.sina.com.cn/mkt/#hs_s # 描述: 中国股票指数数据, 注意该股票指数指新浪提供的国内股票指数 # 限量: 单次返回所有指数的实时行情数据 stock_zh_index_spot_df = ak.stock_zh_index_spot() print(stock_zh_index_spot_df) # 插入到 MySQL 数据库中 common.insert_db(stock_zh_index_spot_df, "stock_zh_index_spot_df", True, "`symbol`")
def get_bank_CurrentQuotes(self): stock_df = ak.stock_zh_index_spot() return stock_df
def stock_zh_index_spot(self): stock_df = ak.stock_zh_index_spot() return stock_df
def query_all_index_spot() -> pd.DataFrame: stock_zh_index_spot_df = ak.stock_zh_index_spot() stock_zh_index_spot_df.rename(columns={'代码': 'code'}, inplace=True) return stock_zh_index_spot_df