본문 바로가기
my_stock/_Technical_Analysis

[Python] Dart 재무재표 분석 _ [1] Dart에서 고유번호 불러오기

by boolean 2021. 8. 20.
728x90

 [Python] Dart 재무재표 분석 _ [1] Dart에서 고유번호 불러오기

현재 문서를 읽기 위한 사전지식

Python 기본 사용법

Python 기본 환경설정

Crawling 의 이해

Dart API 및 API_key 의 이해

Dart Api 에서의 고유번호(corp_code)와 종목코드(stock_code)의 이해

 

Dart에서 Zipfile 형태의 고유번호 xml 리스트 불러와서 UnZip 후 DataFrame 형태로 반환해주기 Python 코드는 다음과 같다

 

"""
Crawling을 위해 사용된 Python내장 페키지들
"""
import requests
import pandas as pd
import io
import zipfile
import xml.etree.ElementTree as et
import json


"""
corp_code를 불러오기 위한 함수 선언
"""
def get_corpcode(crtfc_key):
    params = {'crtfc_key':crtfc_key}
    url = "https://opendart.fss.or.kr/api/corpCode.xml"
    items_en = ["corp_code", "corp_name", "stock_code", "modify_date"]
    
    ## Column명을 한글로 하려면 아래부분 쌍따옴표안을 수정하면 됨
    items_kr = ["corp_code", "corp_name", "stock_code", "modify_date"]
    results  = requests.get(url,params=params)
    uzfile = zipfile.ZipFile(io.BytesIO(results.content))
    final = uzfile.open(uzfile.namelist()[0])
    root = et.fromstring(final.read().decode('utf-8'))
    data = []
    for child in root:
        if len(child.find('stock_code').text.strip()) > 1:
            data.append([])
            for item in items_en:
                data[-1].append(child.find(item).text)
    df = pd.DataFrame(data, columns = items_kr)
    ##print(df)
    return df
    
    
  """
  함수 호출 하기
  """
  crtfc_key = "Dart API 에서 할당받은 개인 Api_key"
  get_corpcode(crtfc_key)

print(df) 해서

출력해보면 다음과 같다.

Dart Api 고유번호(corp_code)

Django를 활용한 재무재표 분석 계획표

1. 회사 고유번호 불러와서 DB에저장하기

    create_model_stock_corpcode.py

2. DB에 저장된 고유번흐를 활용하여 단일회사 상세 재부재표 불러와서 DB에 저장하기

    create_model_stock_single_deepacnt.py

3. DB에 저장된 상세 재무재표를 활용하여 주요 재무재표 추출하여 DB 에 저장하기

    create_model_stock_finance.py

4. DB에 저장된 주요 재무재표를 관심종목 DB에 저장하기

    create_model_stock_final.py

5. DB에 저장된 관심종목 리스트를 화면에 보이게 htlml, view 파일 작성하기

댓글