24 Apr 2020

[Python] 네이버 증권에서 주가 일별시세 정보 가져오기

import requests
from bs4 import BeautifulSoup

def print_stock_price(code, page_num):
    result = [[], [], []]

    for n in range(page_num):
        url = 'https://finance.naver.com/item/sise_day.nhn?code='+code+'&page='+str(n+1)
        r = requests.get(url)
        html = r.content
        soup = BeautifulSoup(html, 'html.parser')
        tr = soup.select('table > tr')

        for i in range(1, len(tr)-1):
            if tr[i].select('td')[0].text.strip():
                result[0].append(tr[i].select('td')[0].text.strip())
                result[1].append(tr[i].select('td')[1].text.strip())
                result[2].append(tr[i].select('td')[6].text.strip())

    for i in range(len(result[0])):
        print(result[0][i], result[1][i], result[2][i])

stock_code = 'xxxxxx'
pages = 100

print_stock_price(stock_code, pages)


결과값: 최근순으로 날짜, 종가, 거래량을 출력

No comments

Copyright © 2018 Mid Air by Ahny. Powered by Blogger.