weather_chat.py 2.87 KB
import requests
import sys
from bs4 import BeautifulSoup
from pprint import pprint

html = requests.get('https://search.naver.com/search.naver?query=날씨')
soup = BeautifulSoup(html.text, 'html.parser')

weather_data = soup.find('div', {'class': 'weather_box'})

# 날씨
currentweather= weather_data.find('p', {'class': 'cast_txt'}).text
weather_endIndex = 0

for i in range(0,len(currentweather)):
    if currentweather[i] == ',':
        weather_endIndex = i
        break        
        
weather = currentweather[0:weather_endIndex]

# 온도
currenttemp_data = weather_data.find('span', {'class':'todaytemp'}).text
currentTemp = int(currenttemp_data)

# 미세먼지 
dust_data = weather_data.findAll('dd')

# 미세먼지, 초미세먼지, 오존지수 상태
dust_txt = dust_data[0].find('span', {'class':'num'}).text
dust = int(dust_txt[0:-3])

if dust > 150:
    dust_status = "매우나쁨"
elif dust > 80:
    dust_status = "나쁨"
elif dust > 30:
    dust_status = "보통"
else:
    dust_status = "좋음"

microdust_txt = dust_data[1].find('span', {'class':'num'}).text
microdust = int(microdust_txt[0:-3])
if microdust > 75:
    microdust_status = "매우나쁨"
elif microdust > 35:
    microdust_status = "나쁨"
elif microdust > 15:
    microdust_status = "보통"
else:
    microdust_status = "좋음"

ozone_txt = dust_data[2].find('span', {'class':'num'}).text
ozone = float(ozone_txt[0:-3])
if ozone > 0.151:
    ozone_status = "매우나쁨"
elif ozone > 0.15:
    ozone_status = "나쁨"
elif ozone > 0.03:
    ozone_status = "보통"
else:
    ozone_status = "좋음"

# 추천 옷차림새
if currentTemp >= 27:
    recDress = "나시티, 반바지, 민소매, 원피스"
elif currentTemp >=23:
    recDress = "반팔, 얇은 셔츠, 얇은 긴팔, 반바지, 면바지"
elif currentTemp >=20:
    recDress = "긴팔티, 가디건, 후드티, 면바지, 슬렉스, 스키니"
elif currentTemp >=17:
    recDress = "니트, 가디건, 후드티, 맨투맨, 청바지, 면바지, 슬랙스, 원피스"
elif currentTemp >= 12:
    recDress = "자켓, 셔츠, 가디건, 간절기 야상, 살색스타킹"
elif currentTemp >=10:
    recDress = "트렌치코트, 간절기 야상, 니트"
elif currentTemp >= 6:
    recDress = "코트, 가죽자켓"
else:
    recDress = "겨울 옷(야상, 패딩, 방한용품)"
    
    
# 특이사항(비/눈)
notice = ""
if '비' in weather:
    notice = "비가 올 가능성이 있습니다. 우산을 챙기세요."
elif '눈' in weather:
    notice = "눈이 올 가능성이 있습니다. 방한 용품을 챙기세요."
else:
    notice = "특이사항이 없습니다."


print('오늘의 온도: '+str(currentTemp)+'도'+
'\n오늘의 날씨: '+weather+'\n미세먼지: '+str(dust)+' '+dust_status+'\n초미세먼지: '+str(microdust)+' '+microdust_status+
'\n오존농도: '+str(ozone)+' '+ozone_status+'\n추천 옷차림새: '+recDress+'\n특이사항: '+notice)