컴퓨터/Python
네이버금융 주식종목 가격가져오기
풍경소리^^
2019. 6. 23. 18:55
https://www.youtube.com/watch?v=1nroPDvUWM4&list=PLAdQRRy4vtQREMg7H7-zFPSOYS6xF1gIL&index=22
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
# 네이버금융 - 리팩토링 function으로 묶기
def get_bs_obj(url):
result=requests.get(url)
bs_obj=BeautifulSoup(result.content,"html.parser")
return bs_obj
# company_code를 받아서 price를 return하는 함수
def get_price(company_code):
url = "https://finance.naver.com/item/main.nhn?code="+company_code
bs_obj=get_bs_obj(url)
no_today=bs_obj.find("p",{"class":"no_today"})
no_today_span=no_today.find("span",{"class":"blind"})
return no_today_span.text
price_samsung=get_price("005930")
price_naver=get_price("035420")
price_debec=get_price("006370")
print(price_debec)