파이썬 주식 / pandas로 상장법인 목록을 읽어와 Listview로 출력하기
1) read_html() 함수로 파일 읽기
한국거래소 기업공시채널(kind.krx.co.kr) 상장법인상세정보 > 상장법인목록에서 excel을 다운받아 read_html() 함수로 상장법인 목록을 읽습니다.
column명이 한글로 되어 있어서 영문으로 변경하였고, 업종코드는 총 6자리로 표시하도록 하였습니다. (26이면 000026으로)
#df = pd.read_html('https://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13')[0]
df = pd.read_html('D:\\test.xls')
dfs = df[0]
print(dfs.columns)
dfs.rename(columns={"회사명":"name"}, inplace=True)
dfs.rename(columns={"종목코드":"code"}, inplace=True)
dfs['code'] = dfs['code'].map('{:06d}'.format)
dfs.rename(columns={"업종":"type"}, inplace=True)
dfs.rename(columns={"주요제품":"major"}, inplace=True)
dfs.rename(columns={"상장일":"start_date"}, inplace=True)
dfs.rename(columns={"결산월":"settlement"}, inplace=True)
dfs.rename(columns={"대표자명":"representative"}, inplace=True)
dfs.rename(columns={"홈페이지":"homepage"}, inplace=True)
dfs.rename(columns={"지역":"region"}, inplace=True)
print(dfs.columns)
2) 가져온 상장법인목록을 Listbox 및 Label로 정보 표시하기
이 내용은 아래 링크를 참조합니다.
https://zidarn87.tistory.com/70?category=415247
전체 코드
import numpy as np
import pandas as pd
from tkinter import *
#df = pd.read_html('https://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13')[0]
df = pd.read_html('D:\\test.xls')
dfs = df[0]
print(dfs.columns)
dfs.rename(columns={"회사명":"name"}, inplace=True)
dfs.rename(columns={"종목코드":"code"}, inplace=True)
dfs['code'] = dfs['code'].map('{:06d}'.format)
dfs.rename(columns={"업종":"type"}, inplace=True)
dfs.rename(columns={"주요제품":"major"}, inplace=True)
dfs.rename(columns={"상장일":"start_date"}, inplace=True)
dfs.rename(columns={"결산월":"settlement"}, inplace=True)
dfs.rename(columns={"대표자명":"representative"}, inplace=True)
dfs.rename(columns={"홈페이지":"homepage"}, inplace=True)
dfs.rename(columns={"지역":"region"}, inplace=True)
print(dfs.columns)
def CurSelect(evt):
value = str((lb.get(lb.curselection())))
index = str((lb.index(lb.curselection())))
print(value)
print(index)
count = lb.index(lb.curselection())
name_w['text'] = dfs.loc[count, 'name']
code_w['text'] = dfs.loc[count, 'code']
type_w['text'] = dfs.loc[count, 'type']
major_w['text'] = dfs.loc[count, 'major']
start_date_w['text'] = dfs.loc[count, 'start_date']
settlement_w['text'] = dfs.loc[count, 'settlement']
representative_w['text'] = dfs.loc[count, 'representative']
homepage_w['text'] = dfs.loc[count, 'homepage']
region_w['text'] = dfs.loc[count, 'region']
window = Tk()
window.geometry("700x400")
lb = Listbox(window, height=5, selectmode=SINGLE)
lb.bind('<<ListboxSelect>>',CurSelect)
lb.pack()
for i in dfs.index:
val = dfs.loc[i, 'name']
lb.insert(END, val)
name_l = Label(window, text="회사명 : ")
name_l.pack()
name_l.place(x=0, y=120, anchor='sw')
name_w = Label(window, text="N/A")
name_w.pack()
name_w.place(x=50, y=120, anchor='sw')
code_l = Label(window, text="종목코드 : ")
code_l.pack()
code_l.place(x=0, y=120+30, anchor='sw')
code_w = Label(window, text="N/A")
code_w.pack()
code_w.place(x=65, y=120+30, anchor='sw')
type_l = Label(window, text="업종 : ")
type_l.pack()
type_l.place(x=0, y=120+30+30, anchor='sw')
type_w = Label(window, text="N/A")
type_w.pack()
type_w.place(x=40, y=120+30+30, anchor='sw')
major_l = Label(window, text="주요제품 : ")
major_l.pack()
major_l.place(x=0, y=120+30+30+30, anchor='sw')
major_w = Label(window, text="N/A")
major_w.pack()
major_w.place(x=65, y=120+30+30+30, anchor='sw')
start_date_l = Label(window, text="상장일 : ")
start_date_l.pack()
start_date_l.place(x=0, y=120+30+30+30+30, anchor='sw')
start_date_w = Label(window, text="N/A")
start_date_w.pack()
start_date_w.place(x=50, y=120+30+30+30+30, anchor='sw')
settlement_l = Label(window, text="결산월 : ")
settlement_l.pack()
settlement_l.place(x=0, y=120+30+30+30+30+30, anchor='sw')
settlement_w = Label(window, text="N/A")
settlement_w.pack()
settlement_w.place(x=50, y=120+30+30+30+30+30, anchor='sw')
representative_l = Label(window, text="대표자명 : ")
representative_l.pack()
representative_l.place(x=0, y=120+30+30+30+30+30+30, anchor='sw')
representative_w = Label(window, text="N/A")
representative_w.pack()
representative_w.place(x=65, y=120+30+30+30+30+30+30, anchor='sw')
homepage_l = Label(window, text="홈페이지 : ")
homepage_l.pack()
homepage_l.place(x=0, y=120+30+30+30+30+30+30+30, anchor='sw')
homepage_w = Label(window, text="N/A")
homepage_w.pack()
homepage_w.place(x=65, y=120+30+30+30+30+30+30+30, anchor='sw')
region_l = Label(window, text="지역 : ")
region_l.pack()
region_l.place(x=0, y=120+30+30+30+30+30+30+30+30, anchor='sw')
region_w = Label(window, text="N/A")
region_w.pack()
region_w.place(x=40, y=120+30+30+30+30+30+30+30+30, anchor='sw')
window.mainloop()