BIG
OpenDartAPI 기업 재무 정보 DBMS insert
DBMS : MariaDB 10.x 를 설치하여 사용하였습니다.
Step.1 DB Table 생성
create table yym_project_eis.OpenDart_02_info_financial
(
od_02_pk bigint auto_increment comment 'pk' primary key,
od_02_corp_code varchar(200) null,
od_02_bsns_year varchar(200) null,
od_02_fs_div varchar(200) null,
od_02_account_nm varchar(200) null,
od_02_account_detail varchar(200) null,
od_02_thstrm_nm varchar(200) null,
od_02_thstrm_amount varchar(200) null,
od_02_currency varchar(200) null
)
comment '재무정보';
Step.2 기업재무정보 수집 및 DB insert
# -*- coding: utf-8 -*-
# OpenDartAPI
# - https://opendart.fss.or.kr/guide/detail.do?apiGrpCd=DS003&apiId=2019020
# - 상장기업 재무정보 : 단일회사 전체 재무제표
import sys
import xml.etree.ElementTree as ET
import mysql.connector
import requests
class ClassAPI_empStatus:
def __init__(self):
self.apiKey = "-------------------------------------------"
self.apiUrl = ""
self.corp_code = ""
self.bsns_year = ""
self.reprt_code = ""
self.fs_div = ""
def set_api_url(self):
url = "https://opendart.fss.or.kr/api/fnlttSinglAcntAll.xml"
url = url + "?crtfc_key=" + self.apiKey
url = url + "&corp_code=" + self.corp_code
url = url + "&bsns_year=" + self.bsns_year
url = url + "&reprt_code=" + self.reprt_code
url = url + "&fs_div=" + self.fs_div
self.apiUrl = url
return url
def call_api(self):
self.set_api_url()
response = requests.get(self.apiUrl)
### http 통신 결과 코드 확인
# print("response.status_code : " + str(response.status_code))
## http 요청이 성공했을때 API의 리턴값을 가져옵니다.
if response.status_code == 200:
rtnString = response.text
return rtnString
if __name__ == '__main__':
dbConn = mysql.connector.connect(
user='--------------'
, password='--------------'
, host='--------------'
, database='--------------'
)
Obj = ClassAPI_empStatus()
# 대상기업 : 삼성전자
Obj.corp_code = "00126380"
# 대상연도 : 2021
Obj.bsns_year = "2021"
# 보고서종류 : 년간사업보고서
Obj.reprt_code = "11011"
# 재무제표 종류 : CFS / OFS
Obj.fs_div = "CFS"
Obj.call_api()
tree = ET.fromstring(Obj.call_api())
ET.dump(tree)
sql = ""
sql = "insert into OpenDart_02_info_financial ("
sql += "od_02_corp_code, od_02_bsns_year, od_02_fs_div, od_02_account_nm, od_02_account_detail, od_02_thstrm_nm, od_02_thstrm_amount, od_02_currency"
sql += ") values (%s, %s, %s, %s, %s, %s, %s, %s );"
print(sql)
with dbConn:
with dbConn.cursor() as cur:
for row in tree.iter("list"):
print("-----------------------------------------------------------------------------------------------")
print("접수번호:" + str(row.find('rcept_no').text))
print("사업년도:" + str(row.find('bsns_year').text))
print("계정명:" + str(row.find('account_nm').text))
print("계정상세:" + str(row.find('account_detail').text))
# print("thstrm_nm:" + str(row.find('thstrm_nm').text))
# print("thstrm_amount:" + str(row.find('thstrm_amount').text))
cur.execute(sql
, (
str(row.find('corp_code').text).strip()
, str(row.find('bsns_year').text).strip()
, str(Obj.fs_div).strip()
, str(row.find('account_nm').text).strip()
, str(row.find('account_detail').text).strip()
, str(row.find('thstrm_nm').text).strip()
, str(row.find('thstrm_amount').text).strip()
, str(row.find('currency').text).strip()
)
)
dbConn.commit()
dbConn.close()
sys.exit(0)
LIST
'!!...Python > !!...OpenDartAPI' 카테고리의 다른 글
[python]OpenDartAPI 기업 직원 현황 정보 DBMS insert (0) | 2022.07.27 |
---|---|
[python]OpenDartAPI 기업코드 DBMS Table insert (0) | 2022.07.23 |
[python]OpenDartAPI 사업보고서 주요내용 - 직원현황 정보 가져오기 (0) | 2022.07.19 |
[python]OpenDartAPI 기업코드 XML Parsing 하기 (0) | 2022.07.18 |