YouTube2Text

[파이썬 퀀트] 18강 - DART API를 이용해 공시정보 및 재무제표 수집하기 — Transcript

by 헨리의 금융MBA · 2,395 words · 354 segments · language en · Watch on YouTube

Full transcript

  1. 0:10Since we have collected the necessary
  2. 0:11stock price and financial statement
  3. 0:13data in the previous sessions, today we
  4. 0:15will learn how to collect corporate
  5. 0:17disclosure filings that can be helpful
  6. 0:19for your investments. If you are an
  7. 0:22investor, you have probably visited
  8. 0:23this site at least once, right? DART is
  9. 0:26an electronic disclosure system managed
  10. 0:28by the Financial Supervisory Service,
  11. 0:31where listed companies submit their
  12. 0:33disclosure documents online, allowing
  13. 0:35investors and users to view them via
  14. 0:37the internet as soon as they are
  15. 0:39submitted. If you search for a company
  16. 0:42you are interested in here, like
  17. 0:44Samsung Electronics, you can see
  18. 0:46various disclosure filings for that
  19. 0:47company. You can look at these to check
  20. 0:52what has been happening, but instead of
  21. 0:55checking the website manually every
  22. 0:58time, you can collect these disclosures
  23. 1:00much more easily using the API. First,
  24. 1:04go to opendart.fss.or.kr here. You will
  25. 1:09need to go in and get an API key. There
  26. 1:12is no separate membership registration
  27. 1:14here; applying for an authentication
  28. 1:16key is essentially the same as signing
  29. 1:19up. So, you just click agree, enter
  30. 1:21your email and password, and your
  31. 1:23registration will be complete. This way
  32. 1:26, you can receive your API key at the
  33. 1:29same time you register. If you sign up
  34. 1:33and log in, you will likely see "
  35. 1:35Approved" under the Authentication Key
  36. 1:37Management section. If you check the
  37. 1:41Open API usage status, it shows how
  38. 1:43many API calls you have made during the
  39. 1:46day, and you can make a total of 20,000
  40. 1:48calls per day. So, you can see that you
  41. 1:52are allowed a very generous amount of
  42. 1:54calls. Now, let’s save the API key
  43. 1:57you received into Python. If you use
  44. 2:01the keyring package, you can manage
  45. 2:03things like passwords and API keys very
  46. 2:05easily. Run this part, and here is the
  47. 2:09service name. Since we are using DART,
  48. 2:12we will set it as "DART API Key," enter
  49. 2:15your name for the username, and input
  50. 2:17the API key for the password. To
  51. 2:21retrieve it after saving, you use
  52. 2:24.get_password and input the service
  53. 2:26name and the corresponding username,
  54. 2:29and the saved password will be called.
  55. 2:33In my case, I saved it as "Henry," so
  56. 2:36it will retrieve the matching API key
  57. 2:39and store it in a variable named "
  58. 2:41api_key." Also, to receive data for
  59. 2:44each company from the Open API, you
  60. 2:46must know the unique code for that
  61. 2:48stock. The method for obtaining that is
  62. 2:51explained in detail here in the
  63. 2:53developer guide. In the developer guide
  64. 2:56, click on disclosure information and
  65. 2:59then go to unique code to see how you
  66. 3:02can retrieve it. The request URL is
  67. 3:05right here, and if you send your API
  68. 3:08key in the request parameter query, you
  69. 3:11will be able to receive the data. So,
  70. 3:14looking at the response results, we see
  71. 3:16something like this, and we'll use it
  72. 3:19to try and download the unique
  73. 3:21identification numbers. First, import
  74. 3:24the necessary packages and create the
  75. 3:27URL according to what's shown on the
  76. 3:30homepage. This is the request URL, and
  77. 3:34you can send your API key in these
  78. 3:36parameters. Then, if you request the
  79. 3:41data using the get function, you get a
  80. 3:44200 code, which means the data has been
  81. 3:47received. And if you check the headers
  82. 3:51section of the received data, you can
  83. 3:53see that a file is attached in the
  84. 3:56attachment section here. Let's look
  85. 3:59into this in more detail. Here, the
  86. 4:03content disposition section, that is.
  87. 4:09If you check this, you can see a
  88. 4:11filename called corp code. You can
  89. 4:14verify that a file is attached. We need
  90. 4:18to unzip this to check the contents of
  91. 4:20the attachment. First, if you enter
  92. 4:23content here, it shows what details are
  93. 4:28included, and we must first convert
  94. 4:30this into a binary stream format. So,
  95. 4:36if you use IO, it converts to a binary
  96. 4:39stream, and since it's a zip file, we
  97. 4:42should unzip it to check. This is how
  98. 4:49you unzip a zip file. So, we will save
  99. 4:55this, and if you run namelist here, you
  100. 5:00can check what files are inside.
  101. 5:04Currently, a file named corp code.xml
  102. 5:07is included. Now, let's load this file
  103. 5:11into Python. You can load the file by
  104. 5:15using read and entering the filename
  105. 5:18here. However, the content is a bit
  106. 5:20garbled. This happens because of an
  107. 5:23encoding error, so let's change the
  108. 5:25encoding to UTF-8 format. Then, when
  109. 5:34you check the data, it is properly
  110. 5:36encoded, and you can see what the
  111. 5:38contents are. Let's save this to this
  112. 5:43section, and since this is in XML
  113. 5:45format, we need to convert it to a
  114. 5:48dictionary format first. First, load
  115. 5:52the package, and doing this will
  116. 6:02convert the XML into a dictionary
  117. 6:03format. After saving it, if you check
  118. 6:08the data, you can see the contents
  119. 6:13inside the list within the dictionary.
  120. 6:16Here, json. Doing this will change it
  121. 6:22to JSON format. And then, here, load it
  122. 6:29using json loads and save this as data
  123. 6:36dict. And then, by calling result and
  124. 6:44then calling list again through get, we
  125. 6:49can extract only the data we want. If
  126. 6:53you check it, you'll see the data has
  127. 6:55been brought in as a series of
  128. 6:57dictionaries. So, finally, you just
  129. 6:59need to turn this into a data frame
  130. 7:01format. If you do this, the unique
  131. 7:12identification numbers will be created
  132. 7:14in a data frame format. I'm not sure
  133. 7:17why they provide it in such a complex
  134. 7:19way, but either way, this process
  135. 7:21allows us to verify a company's unique
  136. 7:23code, and as you can see, there are
  137. 7:25nearly 90,000 pieces of data. There are
  138. 7:2897,000, so why is that? The entries
  139. 7:31where the stock code is marked as "None
  140. 7:33" are not publicly traded stocks. So,
  141. 7:37it includes non-listed companies as
  142. 7:39well, which are actually things we
  143. 7:41don't need. Therefore, we will filter
  144. 7:45out only the listed stocks from this
  145. 7:47list and save them into an SQL database
  146. 7:49. First, we load the SQLAlchemy package
  147. 7:54, find the parts where the stock code
  148. 7:57is not "None" using ` isin` (with a
  149. 8:01tilde for "not"), and then reset the
  150. 8:04index. After that, we'll save it to a
  151. 8:10database named "dart_code" using the
  152. 8:13SQL function. And when we check the SQL
  153. 8:17database, the unique codes for the
  154. 8:26listed stocks appear in the "dart_code"
  155. 8:29table like this. If you are curious
  156. 8:31about Samsung Electronics 'data among
  157. 8:34these, you can just enter it like this,
  158. 8:41right? Then, Samsung Electronics'
  159. 8:44ticker comes up as 005930, and the
  160. 8:46unique code appears as 00126...and so
  161. 8:48on. The reason we need to know this is
  162. 8:51because when we retrieve disclosure
  163. 8:54details from DART, we use this data
  164. 8:56instead of the standard ticker. That is
  165. 8:59why this information is necessary. Now,
  166. 9:02let's get into receiving the disclosure
  167. 9:05data in earnest; first, we will collect
  168. 9:07the disclosure data for all stocks. You
  169. 9:09can find the URL in the developer guide
  170. 9:12here. Click on "Disclosure Information"
  171. 9:15in the developer guide and then click "
  172. 9:17Disclosure Search" to see the relevant
  173. 9:20guide. It shows what parameters are
  174. 9:23required and what kind of response
  175. 9:26results you will get. And if you click
  176. 9:30"Search" here, you can also create a
  177. 9:33sample URL. The part marked as "XX" is
  178. 9:36where you enter your API key, and this
  179. 9:39part is likely the start date. And this
  180. 9:43is the end date, but what about "
  181. 9:46corp_cls"? If you check further up, 'Y'
  182. 9:49means KOSPI, 'K' means KOSDAQ, and if
  183. 9:52you don't enter anything, it queries
  184. 9:55the entire market. Also, for page
  185. 9:59number and page count, it shows 1 and
  186. 10:0110, but the maximum value is actually
  187. 10:04100. So, it would be better to enter
  188. 10:06the maximum value. Therefore, we will
  189. 10:08use this information to create the URL.
  190. 10:11First, we load the package and the date
  191. 10:14. Running ` today () ` gives us today's
  192. 10:17date. And if we use ` relativedelta`,
  193. 10:20that would be subtracting 7 days. So,
  194. 10:23it means from 7 days ago until today,
  195. 10:27and using ` strftime` converts it into
  196. 10:31YYYYMMDD format. Then, for the end date
  197. 10:35, we input today's date and construct
  198. 10:38the URL based on the contents on the
  199. 10:40website. Here is the URL we are
  200. 10:43requesting via an f-string. And you put
  201. 10:46your API key right here. For the start
  202. 10:48date, use the one we created above. And
  203. 10:51for the end date, use the one we
  204. 10:52created above as well. Since we need to
  205. 10:54search both KOSPI and KOSDAQ, we can
  206. 10:58leave corp_cls empty. And you can set
  207. 11:01the page number to 1 and the page count
  208. 11:04to 100. Once this is set, you can
  209. 11:07request the data from this URL using
  210. 11:10the get function. If you see 200, it
  211. 11:14means the data was retrieved
  212. 11:16successfully, and since we requested it
  213. 11:20in JSON format, you can use .json () to
  214. 11:23see the corresponding data. Let's save
  215. 11:28this as 'test' and if you check it,
  216. 11:31you'll see some data in the 'list'
  217. 11:34section. So, we just need to extract
  218. 11:37the 'list' part from this. If we use
  219. 11:40.get ('list') to extract only the list
  220. 11:42portion, the disclosure details will be
  221. 11:47stored in a dictionary format like this
  222. 11:49. We will convert this into a DataFrame
  223. 11:53using the pd.DataFrame function. Then,
  224. 11:56the details for the 100 most recent
  225. 11:59disclosures will be retrieved. But you
  226. 12:02might be interested in the disclosures
  227. 12:05of a specific stock, not all stocks.
  228. 12:08You can also check for those cases.
  229. 12:11According to the development guide, if
  230. 12:13you additionally enter the corp_code,
  231. 12:15which is the unique ID, you can get the
  232. 12:18disclosure details for that specific
  233. 12:20stock. That is when this code is needed
  234. 12:22. This is the one for Samsung
  235. 12:25Electronics, so copy this. Enter the
  236. 12:30Samsung Electronics code into corp_code
  237. 12:33first, and the rest is the same as what
  238. 12:35we saw earlier. The request URL is
  239. 12:38similar, so you just need to add this
  240. 12:40specific part. Just add the query for
  241. 12:43the company's unique ID, and everything
  242. 12:45up to this point is the same as before.
  243. 12:49So, we generate the URL, request the
  244. 12:52data, extract the list, and convert it
  245. 12:56into a DataFrame. If you check the data
  246. 13:00, you will see the disclosure history
  247. 13:02for Samsung Electronics over the past
  248. 13:04week. And by using a for loop to enter
  249. 13:08the codes of the stocks you want, you
  250. 13:11can get the disclosure details only for
  251. 13:14those stocks. Also, if you look at the
  252. 13:18DataFrame here, you'll see some numbers
  253. 13:20, which correspond to the disclosure ID
  254. 13:23. You can even use this to construct a
  255. 13:26URL. To see how the disclosure URL is
  256. 13:30formed, go to the original DART site
  257. 13:32and click here to see the address. This
  258. 13:36part is the fixed address, and this
  259. 13:38part corresponds to the disclosure ID.
  260. 13:42So, you can apply this to create a URL
  261. 13:44for a specific disclosure. First, we
  262. 13:48find the disclosure number at the top
  263. 13:50and use an f-string, which is what we
  264. 13:53saw on the homepage earlier. Then, we
  265. 13:57enter the number we just retrieved into
  266. 13:59the disclosure number field here. If we
  267. 14:02verify this by printing it out, a URL
  268. 14:04is generated. Copy this, paste it into
  269. 14:08your browser, and it will take you
  270. 14:11directly to the disclosure page. This
  271. 14:14allows you to check more detailed
  272. 14:17information about the disclosure
  273. 14:19history. You can also download key
  274. 14:22information from business reports using
  275. 14:25the API. If you click on "Key
  276. 14:28Information from Business Reports" in
  277. 14:29the developer guide, you can see a
  278. 14:31wealth of data. As a sample, let's
  279. 14:35check the details regarding dividends.
  280. 14:39When you click here, it shows you
  281. 14:41exactly what values are required. You
  282. 14:45need the API key, unique code, fiscal
  283. 14:47year, and the report code—1 st
  284. 14:50quarter, half-year, 3rd quarter, and
  285. 14:53annual report. So, by clicking search,
  286. 14:56we send this query to the URL and
  287. 14:58receive the corresponding values in
  288. 15:01JSON format. Let's apply this and try
  289. 15:05to retrieve the dividend details. First
  290. 15:09, we enter the code for Samsung
  291. 15:11Electronics and pull the data for 2021.
  292. 15:14Since 2022 isn't out yet, we'll use
  293. 15:172021 and enter the code for the
  294. 15:19business report. Then, we generate the
  295. 15:23URL using an f-string. After that, the
  296. 15:27process is exactly the same as what we
  297. 15:29looked at earlier. We request the data
  298. 15:31using the get function, extract the
  299. 15:33list portion from the JSON, and convert
  300. 15:36it into a DataFrame. If you check this,
  301. 15:40you can see the dividend-related
  302. 15:43details from Samsung Electronics '2021
  303. 15:46financial statements. Finally, although
  304. 15:50I didn't include it in the book, you
  305. 15:52can also download financial statements
  306. 15:54for all stocks using the API. If you
  307. 15:56click on "Financial Statements of
  308. 15:58Listed Companies" in the developer
  309. 16:00guide, you'll see "All Financial
  310. 16:02Statements for a Single Company."
  311. 16:04However, it does not include the
  312. 16:06financial sector. So, you can view the
  313. 16:08financial statements of all listed
  314. 16:10companies excluding those in the
  315. 16:12finance industry. Clicking the shortcut
  316. 16:15to see how it's structured, you'll find
  317. 16:18that it's the same as what we saw
  318. 16:20before. The only difference is the
  319. 16:22addition of a query to distinguish
  320. 16:24between individual and consolidated
  321. 16:25statements. After checking this, if you
  322. 16:28click search at the bottom to view the
  323. 16:29sample URL, you'll see it looks quite
  324. 16:31similar. It's similar, but just the
  325. 16:34part corresponding to the financial
  326. 16:36statements in the URL changes. So,
  327. 16:38let's try creating a URL using this
  328. 16:41method. Using what's shown on the
  329. 16:43screen, we can create the URL like this
  330. 16:45. Enter your API key, record count, the
  331. 16:49year, the data type you want to see,
  332. 16:51and since we're looking at consolidated
  333. 16:53financials, input OFS here. Then, you
  334. 16:57can just proceed in the same way we did
  335. 17:00before. After doing this and checking
  336. 17:04the data, you can easily retrieve the
  337. 17:06relevant financial statement
  338. 17:09information like this. BS is for the
  339. 17:13balance sheet, IS for the income
  340. 17:15statement, CF for the cash flow
  341. 17:17statement, and CIS for the
  342. 17:18comprehensive income statement, so you
  343. 17:21can easily pull all these financial
  344. 17:23statements as well. As you can see, by
  345. 17:27just changing these parts and modifying
  346. 17:30the relevant query details, you can
  347. 17:32easily receive various data provided by
  348. 17:35DART through the API. And by using the
  349. 17:40Python and Slack integration method I
  350. 17:42uploaded, instead of checking the
  351. 17:44website every time for disclosures, you
  352. 17:46can set it up to automatically send
  353. 17:48disclosures to Slack whenever a company
  354. 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.