weather_info.py
3.55 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
81
82
83
84
85
86
87
88
89
90
91
92
93
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
from pprint import pprint
import requests
html = requests.get('https://search.naver.com/search.naver?query=날씨')
#pprint(html.text)
def weather_info_crawling():
soup = BeautifulSoup(html.text, 'html.parser')
data1 = soup.find('div', {'class': 'weather_box'})
find_address = data1.find('span', {'class':'btn_select'}).text
find_address_info = ('현재 위치: '+find_address + "\n")
find_currenttemp = data1.find('span',{'class': 'todaytemp'}).text
find_currenttemp_info=('현재 온도: '+find_currenttemp+'℃'+ "\n")
data2 = data1.findAll('dd')
find_dust = data2[0].find('span', {'class':'num'}).text
find_dust_info=('현재 미세먼지: '+find_dust + "\n")
result = find_address_info + ' ' + find_currenttemp_info + ' ' + find_dust_info
return result
def find_currenttemp_num():
soup = BeautifulSoup(html.text, 'html.parser')
data1 = soup.find('div', {'class': 'weather_box'})
find_currenttemp = data1.find('span',{'class': 'todaytemp'}).text
find_currenttemp_num = int(find_currenttemp)
result = find_currenttemp_num
return result
#################################################################3
#텔레그램 관련 코드
token = "1721885449:AAHDGMbjSJfhXxML6nfSCpfiU7SghpL_vOE"
id = "1657858421"
bot = telegram.Bot(token)
info_message = '''기능: (#오늘 날씨)''' ########기능 리스트 ##############33
bot.sendMessage(chat_id=id, text=info_message)
react_weather = '''오늘 날씨 봐봐'''
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 ("오늘 날씨" in user_text):
weather_info = weather_info_crawling()
bot.send_message(chat_id=id, text= weather_info)
bot.sendMessage(chat_id=id, text=react_weather)
#bot.send_photo(chat_id=id, photo=open(r'C:\Users\user\Documents\project_opensw-chatbot\telegram_Messenger_Chatbot\img\weather_cloth_info.jpg', 'rb')) ###절대 경로니까 수정하세요!!!!!!
if (find_currenttemp_num()>=27):
bot.send_message(chat_id=id, text="와 미친,, 27도가 넘는데? 낼 무조건 시원하게 얇고 통풍 잘 되는 거 입어, 쪄 죽겠다 ㅋㅋㅋㅋ")
elif (find_currenttemp_num()>=23):
bot.send_message(chat_id=id, text="날씨가 23도 이상이네,,, ㅜㅜ 반팔에 반바지 정도 입으면 충분할 듯!")
elif (find_currenttemp_num()>=20):
bot.send_message(chat_id=id, text="20도 이상이면 밤엔 많이 쌀쌀할 듯 ㅠㅠ,, 진세 감기 조심해!")
elif (find_currenttemp_num()>=15):
bot.send_message(chat_id=id, text="헐 15도 이상이면 가을 날씨네,,,따뜻하게 입고가!!")
else:
update.message.reply_text("꽁꽁 싸매고 나가")
########################################################## 이어서 작업
else:
update.message.reply_text("ㅎㅎ")
echo_handler = MessageHandler(Filters.text, handler)
dispatcher.add_handler(echo_handler)