[파이썬 퀀트] 18강 - DART API를 이용해 공시정보 및 재무제표 수집하기 — Transcript
Full transcript
- 0:10Since we have collected the necessary
- 0:11stock price and financial statement
- 0:13data in the previous sessions, today we
- 0:15will learn how to collect corporate
- 0:17disclosure filings that can be helpful
- 0:19for your investments. If you are an
- 0:22investor, you have probably visited
- 0:23this site at least once, right? DART is
- 0:26an electronic disclosure system managed
- 0:28by the Financial Supervisory Service,
- 0:31where listed companies submit their
- 0:33disclosure documents online, allowing
- 0:35investors and users to view them via
- 0:37the internet as soon as they are
- 0:39submitted. If you search for a company
- 0:42you are interested in here, like
- 0:44Samsung Electronics, you can see
- 0:46various disclosure filings for that
- 0:47company. You can look at these to check
- 0:52what has been happening, but instead of
- 0:55checking the website manually every
- 0:58time, you can collect these disclosures
- 1:00much more easily using the API. First,
- 1:04go to opendart.fss.or.kr here. You will
- 1:09need to go in and get an API key. There
- 1:12is no separate membership registration
- 1:14here; applying for an authentication
- 1:16key is essentially the same as signing
- 1:19up. So, you just click agree, enter
- 1:21your email and password, and your
- 1:23registration will be complete. This way
- 1:26, you can receive your API key at the
- 1:29same time you register. If you sign up
- 1:33and log in, you will likely see "
- 1:35Approved" under the Authentication Key
- 1:37Management section. If you check the
- 1:41Open API usage status, it shows how
- 1:43many API calls you have made during the
- 1:46day, and you can make a total of 20,000
- 1:48calls per day. So, you can see that you
- 1:52are allowed a very generous amount of
- 1:54calls. Now, let’s save the API key
- 1:57you received into Python. If you use
- 2:01the keyring package, you can manage
- 2:03things like passwords and API keys very
- 2:05easily. Run this part, and here is the
- 2:09service name. Since we are using DART,
- 2:12we will set it as "DART API Key," enter
- 2:15your name for the username, and input
- 2:17the API key for the password. To
- 2:21retrieve it after saving, you use
- 2:24.get_password and input the service
- 2:26name and the corresponding username,
- 2:29and the saved password will be called.
- 2:33In my case, I saved it as "Henry," so
- 2:36it will retrieve the matching API key
- 2:39and store it in a variable named "
- 2:41api_key." Also, to receive data for
- 2:44each company from the Open API, you
- 2:46must know the unique code for that
- 2:48stock. The method for obtaining that is
- 2:51explained in detail here in the
- 2:53developer guide. In the developer guide
- 2:56, click on disclosure information and
- 2:59then go to unique code to see how you
- 3:02can retrieve it. The request URL is
- 3:05right here, and if you send your API
- 3:08key in the request parameter query, you
- 3:11will be able to receive the data. So,
- 3:14looking at the response results, we see
- 3:16something like this, and we'll use it
- 3:19to try and download the unique
- 3:21identification numbers. First, import
- 3:24the necessary packages and create the
- 3:27URL according to what's shown on the
- 3:30homepage. This is the request URL, and
- 3:34you can send your API key in these
- 3:36parameters. Then, if you request the
- 3:41data using the get function, you get a
- 3:44200 code, which means the data has been
- 3:47received. And if you check the headers
- 3:51section of the received data, you can
- 3:53see that a file is attached in the
- 3:56attachment section here. Let's look
- 3:59into this in more detail. Here, the
- 4:03content disposition section, that is.
- 4:09If you check this, you can see a
- 4:11filename called corp code. You can
- 4:14verify that a file is attached. We need
- 4:18to unzip this to check the contents of
- 4:20the attachment. First, if you enter
- 4:23content here, it shows what details are
- 4:28included, and we must first convert
- 4:30this into a binary stream format. So,
- 4:36if you use IO, it converts to a binary
- 4:39stream, and since it's a zip file, we
- 4:42should unzip it to check. This is how
- 4:49you unzip a zip file. So, we will save
- 4:55this, and if you run namelist here, you
- 5:00can check what files are inside.
- 5:04Currently, a file named corp code.xml
- 5:07is included. Now, let's load this file
- 5:11into Python. You can load the file by
- 5:15using read and entering the filename
- 5:18here. However, the content is a bit
- 5:20garbled. This happens because of an
- 5:23encoding error, so let's change the
- 5:25encoding to UTF-8 format. Then, when
- 5:34you check the data, it is properly
- 5:36encoded, and you can see what the
- 5:38contents are. Let's save this to this
- 5:43section, and since this is in XML
- 5:45format, we need to convert it to a
- 5:48dictionary format first. First, load
- 5:52the package, and doing this will
- 6:02convert the XML into a dictionary
- 6:03format. After saving it, if you check
- 6:08the data, you can see the contents
- 6:13inside the list within the dictionary.
- 6:16Here, json. Doing this will change it
- 6:22to JSON format. And then, here, load it
- 6:29using json loads and save this as data
- 6:36dict. And then, by calling result and
- 6:44then calling list again through get, we
- 6:49can extract only the data we want. If
- 6:53you check it, you'll see the data has
- 6:55been brought in as a series of
- 6:57dictionaries. So, finally, you just
- 6:59need to turn this into a data frame
- 7:01format. If you do this, the unique
- 7:12identification numbers will be created
- 7:14in a data frame format. I'm not sure
- 7:17why they provide it in such a complex
- 7:19way, but either way, this process
- 7:21allows us to verify a company's unique
- 7:23code, and as you can see, there are
- 7:25nearly 90,000 pieces of data. There are
- 7:2897,000, so why is that? The entries
- 7:31where the stock code is marked as "None
- 7:33" are not publicly traded stocks. So,
- 7:37it includes non-listed companies as
- 7:39well, which are actually things we
- 7:41don't need. Therefore, we will filter
- 7:45out only the listed stocks from this
- 7:47list and save them into an SQL database
- 7:49. First, we load the SQLAlchemy package
- 7:54, find the parts where the stock code
- 7:57is not "None" using ` isin` (with a
- 8:01tilde for "not"), and then reset the
- 8:04index. After that, we'll save it to a
- 8:10database named "dart_code" using the
- 8:13SQL function. And when we check the SQL
- 8:17database, the unique codes for the
- 8:26listed stocks appear in the "dart_code"
- 8:29table like this. If you are curious
- 8:31about Samsung Electronics 'data among
- 8:34these, you can just enter it like this,
- 8:41right? Then, Samsung Electronics'
- 8:44ticker comes up as 005930, and the
- 8:46unique code appears as 00126...and so
- 8:48on. The reason we need to know this is
- 8:51because when we retrieve disclosure
- 8:54details from DART, we use this data
- 8:56instead of the standard ticker. That is
- 8:59why this information is necessary. Now,
- 9:02let's get into receiving the disclosure
- 9:05data in earnest; first, we will collect
- 9:07the disclosure data for all stocks. You
- 9:09can find the URL in the developer guide
- 9:12here. Click on "Disclosure Information"
- 9:15in the developer guide and then click "
- 9:17Disclosure Search" to see the relevant
- 9:20guide. It shows what parameters are
- 9:23required and what kind of response
- 9:26results you will get. And if you click
- 9:30"Search" here, you can also create a
- 9:33sample URL. The part marked as "XX" is
- 9:36where you enter your API key, and this
- 9:39part is likely the start date. And this
- 9:43is the end date, but what about "
- 9:46corp_cls"? If you check further up, 'Y'
- 9:49means KOSPI, 'K' means KOSDAQ, and if
- 9:52you don't enter anything, it queries
- 9:55the entire market. Also, for page
- 9:59number and page count, it shows 1 and
- 10:0110, but the maximum value is actually
- 10:04100. So, it would be better to enter
- 10:06the maximum value. Therefore, we will
- 10:08use this information to create the URL.
- 10:11First, we load the package and the date
- 10:14. Running ` today () ` gives us today's
- 10:17date. And if we use ` relativedelta`,
- 10:20that would be subtracting 7 days. So,
- 10:23it means from 7 days ago until today,
- 10:27and using ` strftime` converts it into
- 10:31YYYYMMDD format. Then, for the end date
- 10:35, we input today's date and construct
- 10:38the URL based on the contents on the
- 10:40website. Here is the URL we are
- 10:43requesting via an f-string. And you put
- 10:46your API key right here. For the start
- 10:48date, use the one we created above. And
- 10:51for the end date, use the one we
- 10:52created above as well. Since we need to
- 10:54search both KOSPI and KOSDAQ, we can
- 10:58leave corp_cls empty. And you can set
- 11:01the page number to 1 and the page count
- 11:04to 100. Once this is set, you can
- 11:07request the data from this URL using
- 11:10the get function. If you see 200, it
- 11:14means the data was retrieved
- 11:16successfully, and since we requested it
- 11:20in JSON format, you can use .json () to
- 11:23see the corresponding data. Let's save
- 11:28this as 'test' and if you check it,
- 11:31you'll see some data in the 'list'
- 11:34section. So, we just need to extract
- 11:37the 'list' part from this. If we use
- 11:40.get ('list') to extract only the list
- 11:42portion, the disclosure details will be
- 11:47stored in a dictionary format like this
- 11:49. We will convert this into a DataFrame
- 11:53using the pd.DataFrame function. Then,
- 11:56the details for the 100 most recent
- 11:59disclosures will be retrieved. But you
- 12:02might be interested in the disclosures
- 12:05of a specific stock, not all stocks.
- 12:08You can also check for those cases.
- 12:11According to the development guide, if
- 12:13you additionally enter the corp_code,
- 12:15which is the unique ID, you can get the
- 12:18disclosure details for that specific
- 12:20stock. That is when this code is needed
- 12:22. This is the one for Samsung
- 12:25Electronics, so copy this. Enter the
- 12:30Samsung Electronics code into corp_code
- 12:33first, and the rest is the same as what
- 12:35we saw earlier. The request URL is
- 12:38similar, so you just need to add this
- 12:40specific part. Just add the query for
- 12:43the company's unique ID, and everything
- 12:45up to this point is the same as before.
- 12:49So, we generate the URL, request the
- 12:52data, extract the list, and convert it
- 12:56into a DataFrame. If you check the data
- 13:00, you will see the disclosure history
- 13:02for Samsung Electronics over the past
- 13:04week. And by using a for loop to enter
- 13:08the codes of the stocks you want, you
- 13:11can get the disclosure details only for
- 13:14those stocks. Also, if you look at the
- 13:18DataFrame here, you'll see some numbers
- 13:20, which correspond to the disclosure ID
- 13:23. You can even use this to construct a
- 13:26URL. To see how the disclosure URL is
- 13:30formed, go to the original DART site
- 13:32and click here to see the address. This
- 13:36part is the fixed address, and this
- 13:38part corresponds to the disclosure ID.
- 13:42So, you can apply this to create a URL
- 13:44for a specific disclosure. First, we
- 13:48find the disclosure number at the top
- 13:50and use an f-string, which is what we
- 13:53saw on the homepage earlier. Then, we
- 13:57enter the number we just retrieved into
- 13:59the disclosure number field here. If we
- 14:02verify this by printing it out, a URL
- 14:04is generated. Copy this, paste it into
- 14:08your browser, and it will take you
- 14:11directly to the disclosure page. This
- 14:14allows you to check more detailed
- 14:17information about the disclosure
- 14:19history. You can also download key
- 14:22information from business reports using
- 14:25the API. If you click on "Key
- 14:28Information from Business Reports" in
- 14:29the developer guide, you can see a
- 14:31wealth of data. As a sample, let's
- 14:35check the details regarding dividends.
- 14:39When you click here, it shows you
- 14:41exactly what values are required. You
- 14:45need the API key, unique code, fiscal
- 14:47year, and the report code—1 st
- 14:50quarter, half-year, 3rd quarter, and
- 14:53annual report. So, by clicking search,
- 14:56we send this query to the URL and
- 14:58receive the corresponding values in
- 15:01JSON format. Let's apply this and try
- 15:05to retrieve the dividend details. First
- 15:09, we enter the code for Samsung
- 15:11Electronics and pull the data for 2021.
- 15:14Since 2022 isn't out yet, we'll use
- 15:172021 and enter the code for the
- 15:19business report. Then, we generate the
- 15:23URL using an f-string. After that, the
- 15:27process is exactly the same as what we
- 15:29looked at earlier. We request the data
- 15:31using the get function, extract the
- 15:33list portion from the JSON, and convert
- 15:36it into a DataFrame. If you check this,
- 15:40you can see the dividend-related
- 15:43details from Samsung Electronics '2021
- 15:46financial statements. Finally, although
- 15:50I didn't include it in the book, you
- 15:52can also download financial statements
- 15:54for all stocks using the API. If you
- 15:56click on "Financial Statements of
- 15:58Listed Companies" in the developer
- 16:00guide, you'll see "All Financial
- 16:02Statements for a Single Company."
- 16:04However, it does not include the
- 16:06financial sector. So, you can view the
- 16:08financial statements of all listed
- 16:10companies excluding those in the
- 16:12finance industry. Clicking the shortcut
- 16:15to see how it's structured, you'll find
- 16:18that it's the same as what we saw
- 16:20before. The only difference is the
- 16:22addition of a query to distinguish
- 16:24between individual and consolidated
- 16:25statements. After checking this, if you
- 16:28click search at the bottom to view the
- 16:29sample URL, you'll see it looks quite
- 16:31similar. It's similar, but just the
- 16:34part corresponding to the financial
- 16:36statements in the URL changes. So,
- 16:38let's try creating a URL using this
- 16:41method. Using what's shown on the
- 16:43screen, we can create the URL like this
- 16:45. Enter your API key, record count, the
- 16:49year, the data type you want to see,
- 16:51and since we're looking at consolidated
- 16:53financials, input OFS here. Then, you
- 16:57can just proceed in the same way we did
- 17:00before. After doing this and checking
- 17:04the data, you can easily retrieve the
- 17:06relevant financial statement
- 17:09information like this. BS is for the
- 17:13balance sheet, IS for the income
- 17:15statement, CF for the cash flow
- 17:17statement, and CIS for the
- 17:18comprehensive income statement, so you
- 17:21can easily pull all these financial
- 17:23statements as well. As you can see, by
- 17:27just changing these parts and modifying
- 17:30the relevant query details, you can
- 17:32easily receive various data provided by
- 17:35DART through the API. And by using the
- 17:40Python and Slack integration method I
- 17:42uploaded, instead of checking the
- 17:44website every time for disclosures, you
- 17:46can set it up to automatically send
- 17:48disclosures to Slack whenever a company
- 17:50you're interested in posts one.
About this transcript
This page contains the full transcript of [파이썬 퀀트] 18강 - DART API를 이용해 공시정보 및 재무제표 수집하기 by 헨리의 금융MBA, generated from the public captions YouTube serves with the video. The transcript has 2,395 words across 354 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.