Seungmi

python 날씨 크롤링

import requests
from bs4 import BeautifulSoup
from pprint import pprint
html = requests.get('https://search.naver.com/search.naver?query=날씨')
soup = BeautifulSoup(html.text, 'html.parser')
data1 = soup.find('div', {'class': 'weather_box'})
address = data1.find('span', {'class': 'btn_select'}).text
find_weather= data1.find('p', {'class': 'cast_txt'}).text
weather_endIndex = 0
for i in range(0,len(find_weather)):
if find_weather[i] == ',':
weather_endIndex = i
break
weather = find_weather[0:weather_endIndex]
notice = ""
if '비' in weather:
notice = "비가 올 가능성이 있습니다. 우산을 챙기세요."
elif '눈' in weather:
notice = "눈이 올 가능성이 있습니다. 방한 용품을 챙기세요."
else:
notice = "특이사항이 없습니다."
find_currenttemp = data1.find('span', {'class':'todaytemp'}).text
currentTemp = int(find_currenttemp)
print('현재 온도: '+find_currenttemp+'도')
\ No newline at end of file