컴퓨터/Python
여러종목 주가 가져오기-python dictionary
풍경소리^^
2019. 6. 23. 22:17
https://www.youtube.com/watch?v=B7pFsal9Zn0&list=PLAdQRRy4vtQREMg7H7-zFPSOYS6xF1gIL&index=25
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
# 네이버금융 - 리팩토링 function으로 묶기
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
# company_code를 받아서 price를 return하는 함수
def get_price(company_code):
bs_obj=get_bs_obj(company_code)
no_today=bs_obj.find("p",{"class":"no_today"})
no_today_span=no_today.find("span",{"class":"blind"})
return no_today_span.text
# print("samsung price : "+get_price("005930"))
# print("debec price : "+get_price("006370"))
# print("hynix price : "+get_price("000660"))
company_codes={"samsung price : ":"005930","debec price : ":"006370","hynix price : ":"000660"}
for company_name,company_price in company_codes.items():
print(company_name+get_price(company_price))