itemList.py 1.75 KB
import pandas as pd

class itemList():
    def __init__(self):
        self.get_kospi()
        print("코스피 종목 수 : ",len(self.code_df_kospi))
        print(self.code_df_kospi)
        self.get_kosdaq()
        print("코스닥 종목 수 : ",len(self.code_df_kosdaq))
        print(self.code_df_kosdaq)

    def get_download_stock(self,market_type=None):
        stock_type={'kospi':'stockMkt','kosdaq':'kosdaqMkt'}
        market_type=stock_type[market_type]
        # 한국거래소 - 상장공시시스템 링크를 이용
        # searchType=13 : 상장기업을 대상으로 조회
        download_link = 'http://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13'
        download_link = download_link + '&marketType=' + market_type
        df=pd.read_html(download_link,header=0)[0]
        return df

    def get_kospi(self):
        self.code_df_kospi=self.get_download_stock('kospi')
        self.code_df_kospi.종목코드=self.code_df_kospi.종목코드.map('{:06d}'.format)                            # 종목코드는 6자리
        self.code_df_kospi=self.code_df_kospi[['회사명','종목코드']]
        self.code_df_kospi = self.code_df_kospi.rename(columns={'회사명': 'code_name', '종목코드': 'code'})    # 한글 컬럼명을 영어로 변경
        return self.code_df_kospi

    def get_kosdaq(self):
        self.code_df_kosdaq=self.get_download_stock('kosdaq')
        self.code_df_kosdaq.종목코드=self.code_df_kosdaq.종목코드.map('{:06d}'.format)
        self.code_df_kosdaq=self.code_df_kosdaq[['회사명','종목코드']]
        self.code_df_kosdaq = self.code_df_kosdaq.rename(columns={'회사명': 'code_name', '종목코드': 'code'})
        return self.code_df_kosdaq

if __name__== "__main__":
    itemList()