josw123 / dart-fss

한국 금융감독원에서 운영하는 다트(Dart) 시스템 크롤링을 위한 라이브러리
https://github.com/josw123/dart-fss
MIT License
321 stars 110 forks source link

NotFoundConsolidated 잡을 수 있는 방법 문의드려요. ㅜㅜ #104

Closed bjkim07 closed 2 years ago

bjkim07 commented 2 years ago

안녕하세요, 파이썬 초보입니다.

아래와 같이 연결재무제표가 없는 경우에는 예외를 잡아서 개별을 받아오려고 하는데 이게 잡히지 않네요.

혹시 어떻게 처리해야 하는지 알려주실 수 있을까요? ㅜㅜ

제 코드는 다음과 같습니다.

            try:
                fs = dart.fs.extract(corp_code=corp_code, bgn_de='20100101', report_tp='quarter', separate=False)
            except (dart.errors.errors.NotFoundConsolidated, dart.errors.NotFoundConsolidated):
                print('에러발생')
                fs = dart.fs.extract(corp_code=corp_code, bgn_de='20100101', report_tp='quarter', separate=True)

그리고 에러는 안잡히고 다음과 같이 계속 진행됩니다.

Annual reports:   0%|          | 0/12 [00:00<?, ?report/s]Traceback (most recent call last):
  File "/Users/leo/.conda/envs/quant_38/lib/python3.8/site-packages/dart_fss/fs/extract.py", line 1381, in extract
    statements = analyze_report(report=report,
  File "/Users/leo/.conda/envs/quant_38/lib/python3.8/site-packages/dart_fss/fs/extract.py", line 1243, in analyze_report
    raise NotFoundConsolidated('Could not find consolidated financial statements')
dart_fss.errors.errors.NotFoundConsolidated: Could not find consolidated financial statements
/Users/leo/.conda/envs/quant_38/lib/python3.8/site-packages/dart_fss/fs/extract.py:1397: RuntimeWarning: Unable to extract financial statements: {'rcp_no': '20210322000910', 'corp_code': '00372873', 'corp_name': 'KTis', 'stock_code': '058860', 'corp_cls': 'Y', 'report_nm': '사업보고서 (2020.12)', 'flr_nm': 'KTis', 'rcept_dt': '20210322', 'rm': ''}.
  warnings.warn(warnings_text, RuntimeWarning)

도움주셔서 감사합니다!

bjkim07 commented 2 years ago

extract.py 에 except Exception 구문 앞에 아래 except 을 추가하여 우선 해결하였습니다. 혹시 저처럼 넘어가지 않으시면 에러를 추가해주세요.

except NotFoundConsolidated: raise

josw123 commented 2 years ago

안녕하세요.

extract.py에 except NotFoundConsolidated를 추가하지 않으셔도 됩니다. 이는 Warning은 발생할지라도 Exception은 전달되기 때문에 이를 아래와 같이 try, except 구문으로 잡아서 사용하시면 됩니다.

from dart_fss.errors import NotFoundConsolidated

try:
    fs = dart.fs.extract('01035289', bgn_de='20210101', separate=False) # 모두투어리츠[01035289] 연결재무제표추출 시도
except NotFoundConsolidated:
    print('연결재무제표가 없으므로 개별재무제표를 추출합니다.')
    fs = dart.fs.extract('01035289', bgn_de='20210101', separate=True) # 모두투어리츠[01035289] 개별재무제표추출
finally:
    print(fs)

아래 extract.py에 추가하지 않은 경우에도 올바르게 NotFoundConsolidated 처리가 가능한것을 확인할 수 있습니다.

image

bjkim07 commented 2 years ago

안녕하세요.

extract.py에 except NotFoundConsolidated를 추가하지 않으셔도 됩니다. 이는 Warning은 발생할지라도 Exception은 전달되기 때문에 이를 아래와 같이 try, except 구문으로 잡아서 사용하시면 됩니다.

from dart_fss.errors import NotFoundConsolidated

try:
    fs = dart.fs.extract('01035289', bgn_de='20210101', separate=False) # 모두투어리츠[01035289] 연결재무제표추출 시도
except NotFoundConsolidated:
    print('연결재무제표가 없으므로 개별재무제표를 추출합니다.')
    fs = dart.fs.extract('01035289', bgn_de='20210101', separate=True) # 모두투어리츠[01035289] 개별재무제표추출
finally:
    print(fs)

아래 extract.py에 추가하지 않은 경우에도 올바르게 NotFoundConsolidated 처리가 가능한것을 확인할 수 있습니다.

image

안녕하세요! API 정말 잘 쓰고 있습니다! 너무 감사드려요!!!!

058860 종목의 경우 문제가 발생합니다. ㅜㅜ raise 구문을 추가하니 문제 없이 작동하였습니다.

사실 저는 파이썬 초보라 이게 왜 그런지 잘 이해가 되지는 않네요. ㅜㅜ

감사합니다.

josw123 commented 2 years ago

확인해보니 오류 발생시 데이터 추출 멈춤을 방지하기 위해 Exception을 warning으로 변경하는 구분이 아래와 같이 추가되어 있습니다.

except Exception as ex :
    traceback.print_exc()
    warnings_text = 'Unable to extract financial statements: {}.'.format(report.to_dict())
    warnings.warn(warnings_text, RuntimeWarning)

다음 버전에는 NotFoundConsolidated 발생시 warning 대신에 Exception을 발생시키는 옵션을 추가하도록 하겠습니다. (혹은 오류 발생시 Exception 발생시키는 옵션을 추가하도록 하겠습니다) 지금 버전에서는 작성하신 바와 같이 코드를 수정하셔서 사용하셔야 될 것 같습니다.

bjkim07 commented 2 years ago

확인해보니 오류 발생시 데이터 추출 멈춤을 방지하기 위해 Exception을 warning으로 변경하는 구분이 아래와 같이 추가되어 있습니다.

except Exception as ex :
    traceback.print_exc()
    warnings_text = 'Unable to extract financial statements: {}.'.format(report.to_dict())
    warnings.warn(warnings_text, RuntimeWarning)

다음 버전에는 NotFoundConsolidated 발생시 warning 대신에 Exception을 발생시키는 옵션을 추가하도록 하겠습니다. (혹은 오류 발생시 Exception 발생시키는 옵션을 추가하도록 하겠습니다) 지금 버전에서는 작성하신 바와 같이 코드를 수정하셔서 사용하셔야 될 것 같습니다.

감사합니다!

좋은 코드 공유해주셔서 감사합니다. 좋은 하루 되셔요.