Micro-sheep / efinance

efinance 是一个可以快速获取基金、股票、债券、期货数据的 Python 库,回测以及量化交易的好帮手!🚀🚀🚀
MIT License
1.81k stars 460 forks source link

ef.stock.get_members(code) not work #76

Open icefairy opened 2 years ago

icefairy commented 2 years ago

code='159915' ef.stock.get_members(code)

not work ,It return an empty dataframe

version:0.4.8

Micro-sheep commented 2 years ago

code='159915' ef.stock.get_members(code)

not work ,It return an empty dataframe

version:0.4.8

This is not index stock, to get it's member, try this

import efinance as ef
ef.fund.get_invest_position('159915')

image

icefairy commented 2 years ago

got it ,thank you!

icefairy commented 2 years ago

execuse me,I can only got 10 items when I use this code: df=ef.fund.get_invest_position('159949')

Can I get all the 50 items in any way please?

icefairy commented 2 years ago

I found it here:http://fundf10.eastmoney.com/ccmx_159949.html and http://www.cnindex.com.cn/module/index-detail.html?act_menu=1&indexCode=399673 but the efinance does not support this yet?

icefairy commented 2 years ago
import requests
import datetime
GZHEADERS={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'}

def getLastMon():
    return datetime.datetime.strftime(datetime.date(*map(int,datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-01').split('-')))-datetime.timedelta(days=1),'%Y-%m')
def getCyb50Members():
    mon=datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m')
    mems=[]
    pn=1
    rows=20
    while True:
        try:
            url=f'http://www.cnindex.com.cn/sample-detail/detail?indexcode=399673&dateStr={mon}&pageNum={pn}&rows={rows}'
            print(url)
            res=requests.get(url=url,headers=GZHEADERS)
            if res.status_code!=200:
                print(f"获取成分股发生网络错误:{res.status_code}")
                return mems
            ret=res.json()
            print(f"js:{ret}")
            if ret['code']==200:
                print(type(ret['data']))
                if pn==1 and ret['total']==0:
                    #本月无数据,尝试获取上个月
                    mon=getLastMon()
                    print(f"本月无数据,尝试获取上个月 mon={mon}")

                elif ret['total']!=0 and ret['total']>len(mems):
                    for it in ret['data']['rows']:
                        mems.append(it)
                    if ret['data']['total']<rows:
                        print('获取完毕')
                        return mems
                    pn=pn+1
                else:
                    print('获取完毕')
                    return mems

            else:
                print(f"获取成分股发发生服务器错误:{ret['code']} msg:{ret['message']}")

        except Exception as e:
            print(f"发生错误:{e}")
lst=getCyb50Members()
print(lst)
print(f'一共获取到:{len(lst)}个成分股')