Open JungHulk opened 1 year ago
# -*- coding: utf-8 -*- """ Created on Tue Dec 27 11:28:16 2022 @author: seungj.jung """ import requests from bs4 import BeautifulSoup import time, os import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # # 네이버 전체 뉴스 url # BASE_URL = "https://finance.naver.com/item/main.nhn" # # get할 때 headers 인자를 이렇게 넣어주면 된다. # requests.get(BASE_URL, headers={'User-Agent':'Mozilla/5.0'}) # headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'} # res = requests.get('',headers=headers ) def get_bs_obj(com_code): url = "https://finance.naver.com/item/main.nhn?code=" + com_code result = requests.get(url, verify=False) bs_obj = BeautifulSoup(result.content, "html.parser") #html.parser 로 파이썬에서 쓸 수 있는 형태로 변환 return bs_obj def get_price(com_code): bs_obj = get_bs_obj(com_code) no_today = bs_obj.find("p", {"class":"no_today"}) blind_now = no_today.find("span", {"class":"blind"}) return blind_now.text def get_percent(com_code): bs_obj = get_bs_obj(com_code) no_exday = bs_obj.find("p", {"class":"no_exday"}) no_up = no_exday.find("em", {"class":"no_up"}) sign = no_exday.find("span", {"class":"ico up"}) text = no_exday.text.split() return text[5], text[6] # Set local time zone to Seoul os.environ['TZ']='Asia/Seoul' #time.tzset() t=time.localtime() # string aa = "002900" bb = "036460" cc = "383220" dd = "009540" while True: print("Tym, ", time.ctime(),", ", get_price(aa)," ",get_percent(aa)[0], get_percent(aa)[1]) # print(get_price("002900")) # print() print("까스, ", time.ctime(),", ", get_price(bb)," ",get_percent(bb)[0], get_percent(bb)[1]) # print(get_price("002900")) print("F, ", time.ctime(),", ", get_price(cc)," ",get_percent(cc)[0], get_percent(cc)[1]) # print(get_price("002900")) # print("쌈바, ", time.ctime(),", ", get_price(cc)," ",get_percent(cc)[0], get_percent(cc)[1]) # print(get_price("002900")) # print("한조, ", time.ctime(),", ", get_price(dd)," ",get_percent(dd)[0], get_percent(dd)[1]) # print(get_price("002900")) # print("ABL, ", time.ctime(),", ", get_price(cc)," ",get_percent(cc)[0], get_percent(cc)[1]) # print(get_price("002900")) print() time.sleep(10) # print("--") # print(get_price("068270")) # print() # print("--") # print(get_price("035720")) # print("=================================") # print() # bs_obj = get_bs_obj("002900") # no_exday = bs_obj.find("p", {"class":"no_exday"}) # no_up = no_exday.find("em", {"class":"no_up"}) # sign = no_exday.find("span", {"class":"ico up"}) # print(sign.text) # # no_up = blind_now.find("span", {"class":"blind"}) # # blind_bar = blind_now.find("span", {"class":"bar"}) # a = no_exday.text # print(a.split( )) # # print(no_exday.text) # # print(sign.text)
https://yjshin.tistory.com/entry/Python-%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EC%9B%B9-%ED%81%AC%EB%A1%A4%EB%A7%81-%EC%8B%A4%EC%8B%9C%EA%B0%84-%EC%A3%BC%EA%B0%80-%EC%A0%95%EB%B3%B4-%EA%B0%80%EC%A0%B8%EC%98%A4%EA%B8%B0