기타/언어

파이썬, 종목 코드를 활용한 네이버 금융 크롤링

영웅시대 2020. 7. 5. 13:19

import requests

from bs4 import BeautifulSoup

 

def get_bs_obj(company_code):

    url = "https://finance.naver.com/item/main.nhn?code=" + company_code

    result = requests.get(url)

    bs_obj = BeautifulSoup(result.content, "html.parser")

    return bs_obj

 

# bs_obj를 받아서 price 를 return하게

def get_price(company_code):

    bs_obj = get_bs_obj(company_code)

    no_today = bs_obj.find ("p", {"class":"no_today"})

    blind_now = no_today.find("span", {"class":"blind"})

    return blind_now.text

 

company_codes = ["005930""017670""000100" ]

# Samsung 005930

# sk telecom 017670

# Youhan

for item in company_codes:

    price = get_price(item)

    print(price)