sharebook-kr / pykrx

KRX 주식 정보 스크래핑
693 stars 240 forks source link

파생상품 전종목기본정보 / 전종목시세 추가 #123

Closed spar7453 closed 1 year ago

spar7453 commented 2 years ago
from pykrx.website.comm.webio import Get
from pykrx.website.krx.krxio import KrxWebIo
from pandas import DataFrame

class DERIVATIVE_전종목기본정보(KrxWebIo):
    class KrxWebIoGet(Get):
        def read(self, **params):
            resp = super().read(**params)
            return resp.json()

        @property
        def url(self):
            return "http://data.krx.co.kr/comm/bldAttendant/executeForResourceBundle.cmd"

    def __init__(self):
        super().__init__()
        self.prods = self.KrxWebIoGet().read(baseName="krx.mdc.i18n.component", key="B107.bld")["result"]["output"]

    @property
    def bld(self):
        return "dbms/MDC/STAT/standard/MDCSTAT12801"

    def fetch(self, prodId: str) -> DataFrame:
        result = self.read(prodId=prodId, csvslx_isNo=False)
        return DataFrame(result["output"])

    def fetch_all(self) -> DataFrame:
        df = DataFrame()
        for p in self.prods:
            df = df.append(self.fetch(p["value"]), ignore_index=True)
        return df

class DERIVATIVE_전종목시세(KrxWebIo):
    class KrxWebIoGet(Get):
        def read(self, **params):
            resp = super().read(**params)
            return resp.json()

        @property
        def url(self):
            return "http://data.krx.co.kr/comm/bldAttendant/executeForResourceBundle.cmd"

    def __init__(self):
        super().__init__()
        self.prods = self.KrxWebIoGet().read(baseName="krx.mdc.i18n.component", key="B107.bld")["result"]["output"]

    @property
    def bld(self):
        return "dbms/MDC/STAT/standard/MDCSTAT12501"

    def fetch(self, trdDd: str, prodId: str) -> DataFrame:
        result = self.read(
            trdDd=trdDd,
            prodId=prodId,
            trdDdBox1=trdDd,
            trdDdBox2=trdDd,
            mktTpCd="T",
            rghtTpCd="T",
            share="1",
            money="1",
            csvslx_isNo=False,
        )
        return DataFrame(result["output"])

    def fetch_all(self, trdDd: str) -> DataFrame:
        df = DataFrame()
        for p in self.prods:
            df = df.append(self.fetch(trdDd, p["value"]), ignore_index=True)
        return df

if __name__ == "__main__":
    import pandas as pd

    pd.set_option("display.width", None)
    print(DERIVATIVE_전종목기본정보().fetch_all())
    print(DERIVATIVE_전종목시세().fetch_all("20220602"))
mr-yoo commented 2 years ago

작성해주신 코드를 기반으로 기능을 추가하고 있습니다.

from pykrx import future

tickers = get_future_ticker_list()
for t in tickers:
    df = get_future_ohlcv("20220902", t)
    print(df)