Python/파이썬과 주식, 코인

네이버 금융(시세)에서 주가 갈무리(크롤링)하기

컴닥 2019. 11. 6. 23:36
반응형

* 투자용으로 사용하지 마십시오. 

 

네이버 금융의 개별 종목에는 시세라는 항목이 있습니다. 

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

 

쭉 내려보면 일별 시세라는 항목이 있습니다. 

ㅇㅇ 이걸 크롤링하면 되겠죠?

 

이 항목의  HTML을 보면 iframe로 되어 있네요. 

 

<iframe name="day" src="/item/sise_day.nhn?code=005930" width="100%" height="360" marginheight="0" bottommargin="0" topmargin="0" scrolling="no" frameborder="0" title="일별 시세"></iframe>

iframe의 주소는 src="/item/sise_day.nhn?code=005930" 입니다. 

 

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

 

즉 위 주소를 방문해보면 다음 화면을 볼 수 있는 거죠. 

 

아래에 페이지 목록을 클릭해보면 주소가 다음과 같이 변합니다. 

https://finance.naver.com/item/sise_day.nhn?code=005930&page=1

 

이제 코딩을 하면 됩니다. 

import pandas as pd

code = '005930'
page = '1'
url = f'https://finance.naver.com/item/sise_day.nhn?code={code}&page={page}'

temp = pd.read_html(url)[0]
del temp['전일비']
temp = temp.loc[temp["날짜"].isnull() == False]
temp['종가'] = temp['종가'].astype(int)
temp['시가'] = temp['시가'].astype(int)
temp['고가'] = temp['고가'].astype(int)
temp['저가'] = temp['저가'].astype(int)
temp['거래량'] = temp['거래량'].astype(int)
print(temp)

# astype(np.int64) 넘파이를 불러서 형변환을 해도 좋습니다. 

 

조금 더 응용하면 마지막 페이지까지 한번에 갈무리할 수도 있습니다. ^^

숙제로 남겨드리겠습니다.

 

 

그런데, 앞서 설명한 차트(https://comdoc.tistory.com/entry/네이버-금융차트에서-주가-갈무리크롤링하기)의 데이터와 시세의 데이터를 비교해 보면 숫자가 다릅니다. OTL

 

 

삼성전자가 주식분할을 하기 전인 18년 4월 27일의 데이터를 비교해 보면 시세의 데이터는 보정이 안된 데이터임을 알 수 있습니다. 보정이 안된 데이터로는 비교가 어렵습니다. ㅠ,.ㅠ 

반응형