import time import requests from bs4 import BeautifulSoup from datetime import datetime from stock.util.SlackBot import SlackBot class HTS_Alert : slackBot = None def __init__(self): self.slackBot = SlackBot() return def checkPrice(self, code): code_index = 0 PERCENT = ["40%", "30%", "30%"] CHECK_PRICE = [2670, 2620, 2580] THIS_TIME = datetime.now() today = datetime.today().strftime('%Y%m%d') while datetime.strptime(today + " 090000", '%Y%m%d %H%M%S') < THIS_TIME < datetime.strptime(today + " 151500", '%Y%m%d %H%M%S'): url = 'https://finance.naver.com/item/main.naver?code={code}'.format(code=code.strip()) res = requests.get(url) html = res.text soup = BeautifulSoup(html, 'html.parser') post = soup.select_one('#chart_area > .rate_info > .today > .no_today > em') arr = post.text.split("\n") c_price = int(arr[1].replace(',', '')) if c_price < CHECK_PRICE[code_index]: self.slackBot.sendMsg(code + " " + str(CHECK_PRICE[code_index]) + " " + PERCENT[code_index] + "를 파세요...") code_index += 1 if code_index > 2: break time.sleep(5) return if __name__ == "__main__": hTS_Alert = HTS_Alert() post = hTS_Alert.checkPrice('252670') print ("done...")