covid.py
2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import telegram
from telegram.ext import Updater
from telegram.ext import MessageHandler, Filters
from bs4 import BeautifulSoup
from selenium import webdriver
import urllib.request as req
import os
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
#크롤링 관련 함수
def covid_num_crawling():
code = req.urlopen("https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=0&ie=utf8&query=%ED%99%95%EC%A7%84%EC%9E%90")
soup = BeautifulSoup(code, "html.parser")
info_num = soup.select("div.status_today em")
result = int(info_num[0].string) + int(info_num[1].string)
return result
def covid_news_crawling():
code = req.urlopen("https://search.naver.com/search.naver?where=news&sm=tab_jum&query=%EC%BD%94%EB%A1%9C%EB%82%98")
soup = BeautifulSoup(code, "html.parser")
title_list = soup.select("a.news_tit")
output_result = ""
for i in title_list:
title = i.text
news_url = i.attrs["href"]
output_result += title + "\n" + news_url + "\n\n"
if title_list.index(i) == 2:
break
return output_result
#텔레그램 관련 코드
token = "1721885449:AAHDGMbjSJfhXxML6nfSCpfiU7SghpL_vOE"
id = "1657858421"
bot = telegram.Bot(token)
info_message = '''진세 진세, 오늘 코로나 확진자 수 몇명인지 알아?? 완전 무섭다,,, (#몇명인데?)'''
bot.sendMessage(chat_id=id, text=info_message)
news_message = '''관련 뉴스도 엄청 많이 보도되고 있더라,,, (#뉴스)'''
worried_message = '''진세도 꼭 마스크 끼고 손 씻고 주의해!! (#웅웅 너도)'''
react01_message = '''오키 ㅎㅎ'''
updater = Updater(token=token, use_context=True)
dispatcher = updater.dispatcher
updater.start_polling()
# 챗봇 응답
def handler(update, context):
user_text = update.message.text # 사용자가 보낸 메세지를 user_text 변수에 저장합니다.
# 오늘 확진자 수 답장
if (user_text == "몇명인데?"):
covid_num = covid_num_crawling()
bot.send_message(chat_id=id, text="오늘 확진자 수 : {} 명".format(covid_num))
bot.sendMessage(chat_id=id, text=news_message)
# 코로나 관련 뉴스 답장
elif (user_text == "뉴스"):
covid_news = covid_news_crawling()
bot.send_message(chat_id=id, text=covid_news)
bot.sendMessage(chat_id=id, text=worried_message)
elif (user_text == "웅웅 너도"):
bot.sendMessage(chat_id=id, text=react01_message)
echo_handler = MessageHandler(Filters.text, handler)
dispatcher.add_handler(echo_handler)