반응형

[Python 재무제표 크롤링 #6] NAVER 금융에서 재무제표 HTML 요소 추출하기

반응형

| NAVER 금융 HTML문서 가져오기

 


이번 시간에는 우리나라 시가총액 1위인 삼성전자의 재무제표를 크롤링하겠습니다. 먼저 네이버 금융 사이트에 들어갑니다. 네이버 금융의 검색창에서 삼성전자를 검색합니다.



검색한 뒤 삼성전자의 주식정보 페이지에 들어가면 다음과 같은 URL로 검색 되어 있는 것을 볼 수 있습니다. 



"https://finance.naver.com/item/main.nhn?code=005930"


이 URL을 가지고 현재 사이트의 HTML 문서를 RequestsBeautifulSoup 라이브러리를 이용해 가져올 것입니다. 다음 코드를 이용해서요

 

import requests
from bs4 import BeautifulSoup

URL = "https://finance.naver.com/item/main.nhn?code=005930"

samsung_electronic = requests.get(URL)
html = samsung_electronic.text

soup = BeautifulSoup(html, 'html.parser')

▶[Python/Python 재무제표 크롤링] - [Python 재무제표 크롤링] Requests, BeautifulSoup로 크롤링(Crawling), 데이터 추출하기(Data Extraction) - 1

▶[Python/Python 재무제표 크롤링] - [Python 재무제표 크롤링] Requests, BeautifulSoup로 크롤링(Crawling), 데이터 추출하기(Data Extraction) - 2


 


| HTML문서에서 재무제표를 구성하는 HTML요소 추출하기


Requests 라이브러리를 이용해서 현재 삼성전자 주식정보창의 HTML을 가져오면 5000줄 이상의 HTML를 가져오게 됩니다. 하지만 이 HTML 데이터는 다 필요가 없습니다. 왜냐하면 이 HTML 문서에서 다음과 같은 재무제표 정보만 필요하기 때문이죠.



위 그림에 해당되는 HTML 요소들이 무엇인지 파악하기 위해서는 각 브라우저에 내장되어 있는 개발자 도구를 이용하여 빠르고 쉽게 해당 페이지 부분의 HTML 요소를 캐치할 수 있습니다.


여기서는 크롬을 기준으로 설명하겠습니다. 오른쪽 상단에 있는 메뉴에 있는 개발자 도구를 클릭하면 



아래 그림의 왼쪽 부분과 같이 개발자 도구 창이 올라옵니다. ( 설정에 따라서 아래 창에 떠질 수 있습니다) 해당 요소들을 오버랩하면 오른쪽 페이지 부분에 해당 HTML 요소가 어떤 부분을 구성하는 지에 대한 정보를 손쉽게 알 수 있습니다.  


이 편리한 도구를 이용해서 재무제표 정보가 어느 HTML요소로 구성되어 있는 지 찾습니다. 분석해본 결과


div tag의 "section.cop_analysis"인 요소(다중 클래스 요소) 안의 클래스가 "sub_section"인 요소가 재무제표를 구성하는 HTML 요소군요.


CSS selector로 나타내면 다음과 같습니다.


div.section.cop_analysis div.sub_section 

 



위 ccs selector를 이용해서 html 부분을 추출하는 코드입니다.

finance_html = soup.select('div.section.cop_analysis div.sub_section')

print(finance_html)

<div class="sub_section">
<table class="tb_type1 tb_num tb_type1_ifrs" summary="기업실적분석에 관한표이며 주요재무정보를 최근 연간 실적, 분기 실적에 따라 정보를 제공합니다.">
<caption>기업실적분석 테이블</caption>
<colgroup>
<col width="86"/>
<col width="59"/>
<col width="59"/>
<col width="59"/>
<col width="62"/>
<col width="58"/>
<col width="58"/>
<col width="58"/>
<col width="58"/>
<col/>
</colgroup>
<thead>
<tr class="t_line">
<th class="h_th2 th_cop_anal5 b_line" rowspan="3"><strong>주요재무정보</strong></th>
<th class="h_th2 th_cop_anal6" colspan="4" scope="col"><strong>최근 연간 실적</strong></th>
<th class="h_th2 th_cop_anal7 last" colspan="6" scope="col"><strong>최근 분기 실적</strong></th>
</tr>
<tr>
<th class="" scope="col">
2015.12
</th>
<th class="" scope="col">
2016.12
</th>
<th class="" scope="col">
2017.12
</th>
<th class="t_line cell_strong" scope="col">
2018.12<em>(E)</em>
</th>
<th class="" scope="col">
2017.09
</th>
<th class="" scope="col">
2017.12
</th>
<th class="" scope="col">
2018.03
</th>
<th class="" scope="col">
2018.06
</th>
<th class="" scope="col">
2018.09
</th>
...
<td class="no_data 2 last cell_strong">
</td>
</tr>
<tr>
<th class="h_th2 th_cop_anal23" scope="row"><strong>배당성향(%)</strong></th>
<td class="">


16.42


</td>
<td class="">


17.81


</td>
<td class="">


14.09

... (생략)

</td>
<td class="no_data 2 t_line cell_strong">
</td>
<td class="no_data 2">
</td>
<td class="no_data 2">
</td>
<td class="no_data 2">
</td>
<td class="no_data 2">
</td>
<td class="no_data 2">
</td>
<td class="no_data 2 last cell_strong">
</td>
</tr>
</tbody>
</table>
</div>]


| 전체 코드

import requests
from bs4 import BeautifulSoup

URL = "https://finance.naver.com/item/main.nhn?code=005930"

samsung_electronic = requests.get(URL)
html = samsung_electronic.text

soup = BeautifulSoup(html, 'html.parser')

finance_html = soup.select('div.section.cop_analysis div.sub_section')

print(finance_html)



반응형

이 글을 공유하기

댓글

Designed by JB FACTORY