Python获取全部场外基金/ETF/QDII/REITS代码信息

888 阅读1分钟

image.png

# 保存基金信息至本地
def save_funds():
    all_funds = target_util._get_all_funds()
    with open("fund.csv", 'a+') as f:
        f.write("基金代码,基金名称,市场,分类,类型\n")
        for fund in all_funds:
            f.write("{fund[id]},{fund[name]},{fund[category]},{fund[tag]},{fund[type]}\n".format(
                fund=fund
            ))

    logging.info("全部基金信息写入完成!")
    
    

if __name__ == "__main__":
    save_funds()

# 获取全部基金代码及名称
def _get_all_funds():
    base_url = "http://fund.eastmoney.com/js/fundcode_search.js"
    resp = requests.get(base_url)
    if not resp.ok:
        logging.error("全部基金信息请求失败:{0}".format(base_url))
    resp_str = resp.text.replace("var r = ", "").replace(";","")
    funds = ast.literal_eval(resp_str)

    all_funds = []
    for fund in funds:
        # info = ast.literal_eval(fund)
        all_funds.append({
            "id": fund[0],
            "name": fund[2],
            "category": "公募基金",
            "tag": fund[3],
            "type": "基金"
        })

    logging.warning("全部基金信息共{0}条。".format(len(all_funds)))
    return all_funds