이하영

기업목록 받아오기

1 +import pandas as pd
2 +
3 +class itemList():
4 + def __init__(self):
5 + self.get_kospi()
6 + print("코스피 종목 수 : ",len(self.code_df_kospi))
7 + print(self.code_df_kospi)
8 + self.get_kosdaq()
9 + print("코스닥 종목 수 : ",len(self.code_df_kosdaq))
10 + print(self.code_df_kosdaq)
11 +
12 + def get_download_stock(self,market_type=None):
13 + stock_type={'kospi':'stockMkt','kosdaq':'kosdaqMkt'}
14 + market_type=stock_type[market_type]
15 + # 한국거래소 - 상장공시시스템 링크를 이용
16 + # searchType=13 : 상장기업을 대상으로 조회
17 + download_link = 'http://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13'
18 + download_link = download_link + '&marketType=' + market_type
19 + df=pd.read_html(download_link,header=0)[0]
20 + return df
21 +
22 + def get_kospi(self):
23 + self.code_df_kospi=self.get_download_stock('kospi')
24 + self.code_df_kospi.종목코드=self.code_df_kospi.종목코드.map('{:06d}'.format) # 종목코드는 6자리
25 + self.code_df_kospi=self.code_df_kospi[['회사명','종목코드']]
26 + self.code_df_kospi = self.code_df_kospi.rename(columns={'회사명': 'code_name', '종목코드': 'code'}) # 한글 컬럼명을 영어로 변경
27 + return self.code_df_kospi
28 +
29 + def get_kosdaq(self):
30 + self.code_df_kosdaq=self.get_download_stock('kosdaq')
31 + self.code_df_kosdaq.종목코드=self.code_df_kosdaq.종목코드.map('{:06d}'.format)
32 + self.code_df_kosdaq=self.code_df_kosdaq[['회사명','종목코드']]
33 + self.code_df_kosdaq = self.code_df_kosdaq.rename(columns={'회사명': 'code_name', '종목코드': 'code'})
34 + return self.code_df_kosdaq
35 +
36 +if __name__== "__main__":
37 + itemList()
...\ No newline at end of file ...\ No newline at end of file