삼성전자 주가 수집하기(크롤링)

KyungOok,Sung
Jun 29, 2021

--

import pandas as pd
from urllib.request import urlopen
from bs4 import BeautifulSoup
from matplotlib import pyplot as plt
# https://finance.naver.com/item/main.nhn?code=005930

last page 번호 찾기

아래 그림 형관펜 ‘맨뒤’에 해당하는 page 번호 찾기

1페이지 부터 마지막 페이지까지 for문을 돌면서 데이터를 다운로드 하기 위함

html 내부는 아래와 같다

<td class="pgRR">
<a href="/item/sise_day.nhn?code=005930&amp;page=615">맨뒤
<img src="https://ssl.pstatic.net/static/n/cmn/bu_pgarRR.gif" width="8" height="5" alt="" border="0">
</a>
</td>

시도 1 — 실패

크롤링을 막은 것으로 보인다

url = 'https://finance.naver.com/item/sise_day.nhn?code=005930&page=1'
doc = urlopen(url)
html = BeautifulSoup(doc, 'lxml')
# print(html)

html을 print 하면 아래와 같은 에러가 발생한다

시도 2 > 성공

headers를 추가한다. 인터넷브라우저를 활용해 조회하는 것을 모사한다

= 을 기준으로 split 해 마지막에 있는 요소를 찾는다.

# 1) = 을 기준으로 split 해 마지막에 있는 요소를 찾는다

전체 데이터 내려 받기

1) Test

2) final 버전

csv 파일에 저장
1111

headers 정보 알아내기

m.avalon.co.kr/check.html

#df.shapedf.head()
png
df.tail()
png
df.describe()
png
png

--

--

No responses yet