ricequant股东人数数据获取方式

 导入程序包

import pandas as pd
import numpy as np
from datetime import datetime
import json
import requests

核心api

def get_share_holder_cache(order_book_ids):
    ret = {}
    for order_book_id in order_book_ids:
        print(order_book_id)
        old_book_id = order_book_id
        order_book_id = order_book_id[:-5]
        if order_book_id[:2] == "60"or order_book_id[:2] == "68":
            order_book_id = "sh" + order_book_id
        if order_book_id[:2] == "30" or order_book_id[:1] == "0":
            order_book_id = "sz" + order_book_id
        
        try:
            data_url = "https://emweb.securities.eastmoney.com/PC_HSF10/ShareholderResearch/PageAjax?code=" + order_book_id
            response = requests.post(data_url)
            _dict = json.loads(response.text)["gdrs"]
            ret[old_book_id] = _dict
        except:
            pass
        
    return ret
    
def get_share_holder(share_holder_cache, order_book_id, date):
    date = datetime.strptime(date,"%Y-%m-%d")
    if order_book_id not in share_holder_cache:
        return None
    _dict = share_holder_cache[order_book_id]
    ret = None
    for item in _dict:
        tmp_date = datetime.strptime(item["END_DATE"][:10], "%Y-%m-%d")
        if (date - tmp_date).days > 0:
            ret = item
            break
    return ret

 示例demo

holder_cache = get_share_holder_cache(["603685.XSHG"])
x  = get_share_holder(holder_cache, "603685.XSHG", "2022-08-03")
if x == None:
    print("not find")
else:
    print(x)

以下是结合平台api进行股票的提取,和文件的保存

share_holder_storage = {}
stocks = all_instruments(type="CS", market='cn')["order_book_id"].tolist()#industry('C39') #+ industry("I65") + industry("C27") + industry("C36") + industry("C38")
cache = get_share_holder_cache(stocks)

文件保存的目的,是为了后续使用缓存,提高获取数据速率

import pickle
# 保存模型
with open('share_holder.txt', 'wb') as f:
    pickle.dump(cache, f)

# 读取模型
with open('share_holder.txt', 'rb') as f:
    new_cache = pickle.load(f)
    
get_share_holder(cache, "000016.XSHE", "2022-08-03")

你可能感兴趣的:(python,量化交易,获取股东人数,数据爬取)