def get_Topjijin(self): repsponse = ak.fund_em_open_fund_rank() print(repsponse.head(21)) return repsponse.head(21) #获取基金排名 def get_Topjijin(self): repsponse = ak.fund_em_open_fund_rank() print(repsponse.head(21)) return repsponse.head(21)
def getFundHoldingHK(pdate:dt): ''' 生成持仓为港股通的基金列表 ''' rDate=reportDate() hkQuote = pd.read_csv('md/hk' + pdate.strftime('%Y%m%d') + '_Bak.csv', encoding='GBK') df = ak.fund_em_open_fund_rank() df.drop('序号', 1, inplace=True) df = df[df['基金简称'].str.contains('港股|香港')] fundlist = df[['基金代码','基金简称']].copy().set_index('基金代码') df['基金简称'] = df.apply( lambda x: '<a href="http://fund.eastmoney.com/{fundcode}.html">{fundname}</a>'.format(fundcode=x['基金代码'], fundname=x['基金简称']), axis=1) df['基金代码'] = df['基金代码'].apply( lambda x: '<a href="https://xueqiu.com/S/F{fundcode}">{fundcode}</a>'.format(fundcode=x)) df['weekday']=None for i in range(0, 5): for calKey in ['_U','_J']: # 加入url参数(小时),让浏览器不使用缓存 filename = '../etrobot.github.io/Quant/%s%s%s.html' % ('hk', i + 1, calKey) if os.path.isfile(filename): with open(filename, "r") as f: output = re.findall('\?t=.*"', f.read()) renderHtml(df, '../CMS/source/Quant/fundhk.html', '持仓港股的内地基金' + rDate.strftime('%y%m%d')) fname='fund/hk'+rDate.strftime('%Y%m%d')+'.csv' if os.path.isfile(fname): return pd.read_csv(fname,dtype={'fundCode':str}) mlog('港股基金爬取中...') df_hk_holding=pd.DataFrame() for fundCode in fundlist.index: df_holding=ak.fund_em_portfolio_hold(code=fundCode, year=str(rDate.year)) if len(df_holding)==0: continue df_hk=df_holding[df_holding['股票名称'].isin(hkQuote['name'])] df_hk['股票代码'] = [x[1:] for x in df_hk['股票代码'].values] df_hk['fundCode']=fundCode df_hk['fundName']=fundlist.at[fundCode,'基金简称'] df_hk_holding=df_hk_holding.append(df_hk) df_hk_holding.to_csv(fname) return df_hk_holding
def heldBy(symbol:str,pdate:dt,mkt='cn'): ''' 获取持仓当前股票的基金列表 :param symbol: :return list: ''' if mkt == 'cn': symbol=symbol[-6:] furl='http://data.eastmoney.com/dataapi/zlsj/detail?SHType=1&SHCode=&SCode=%s&ReportDate=%s&sortField=FREE_SHARES_RATIO&sortDirec=1&pageNum=1&pageSize=290'%(symbol,reportDate().strftime('%Y-%m-%d')) response = requests.get(url=furl, headers={"user-agent": "Mozilla"}) data=pd.DataFrame(json.loads(response.text)['data']) if len(data) > 0: flist = data[data['PARENT_ORGCODE_OLD'].isin(getFundListSorted()[:30])]['HOLDER_CODE'].to_list() else: return None elif mkt=='hk': data=getFundHoldingHK(pdate) if len(data) > 0: flist = data[data['股票代码']==symbol]['fundCode'].to_list() else: return None else: return None fname='./fund/'+pdate.strftime('%Y%m%d')+'.csv' if os.path.isfile(fname): df=pd.read_csv(fname,dtype={'基金代码':str}) else: df=ak.fund_em_open_fund_rank() df.drop('序号',1,inplace=True) df.to_csv(fname,index=False) cols=['单位净值','累计净值','日增长率','近1周','近1月','近3月','近6月','近1年','近2年','近3年','今年来','成立来','自定义'] df[cols] = df[cols].apply(pd.to_numeric, errors='coerce', axis=1) df=df[df['基金代码'].isin(flist)].sort_values(by=['近1月', '近1周'], ascending=False) if len(df) == 0: return df df['基金简称'] = df.apply(lambda x: '<a href="http://fund.eastmoney.com/{fundcode}.html">{fundname}</a>'.format(fundcode=x['基金代码'],fundname=x['基金简称']), axis=1) df['基金代码'] = df['基金代码'].apply( lambda x: '<a href="https://xueqiu.com/S/F{fundcode}">{fundcode}</a>'.format(fundcode=x)) return df
#!/usr/bin/python import akshare as ak stock = 'sh000001' #data = ak.stock_zh_index_daily(stock) data = ak.fund_em_open_fund_rank() print(data)
def extract_fund_rank(fund_type, sort_by, sort_method): fund_em_open_fund_rank_df = ak.fund_em_open_fund_rank(symbol=fund_type) for each in sort_by: fund_em_open_fund_rank_df[each] = fund_em_open_fund_rank_df[each].replace('',np.nan).astype(float) return fund_em_open_fund_rank_df.sort_values(by=sort_by,ascending = sort_method)