웹크롤링 강좌 – 기상청의 단기예보 가져오기Python/웹크롤링2023. 10. 22. 05:44
Table of Contents
반응형
뷰티플수프를 사용하여 기상청의 단예보를 웹크롤링합니다.유튜브 영상 제작할때와 웹페이지가 많이 달라졌습니다.
영상을 무시하시고 아래 코드만 살펴보세요.
2020. 03. 10 최초작성
2022. 12. 19
2023. 5. 21 검증
from urllib.request import urlopen from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager from bs4 import BeautifulSoup from urllib.request import urlopen import datetime driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get("https://www.weather.go.kr/weather/forecast/timeseries.jsp") bsObject = BeautifulSoup(driver.page_source, 'html.parser') date = datetime.datetime.now().strftime("%Y-%m-%d") for item in bsObject.find_all('ul', {'data-date':date}): data = item.select('li') # 시각 time = data[0].contents[1].string # 날씨 weather = data[1].contents[1].string # 기온 temperature = data[2].contents[1].contents[0].string # 체감온도 sensible_temperature = data[3].contents[1].string # 강수량 amount_of_precipitation = data[4].contents[1].string # 강수확률 probability_of_precipitation = data[5].contents[1].string # 바람 wind = data[6].contents[1].string + ' ' + data[6].contents[2].contents[0].string + data[6].contents[2].contents[1].string # 습도 humidity = data[7].contents[1].string # 한파영향 cold_surge_effect = data[8].contents[1].string print(time, weather, temperature, sensible_temperature, amount_of_precipitation, probability_of_precipitation, wind, humidity, cold_surge_effect) |
실행결과
09시 흐림 15℃ 17℃ - 남서풍 2m/s 85% -
10시 흐림 18℃ 19℃ - 남서풍 3m/s 70% -
11시 흐림 19℃ 19℃ - 남서풍 3m/s 55% -
12시 흐림 21℃ 20℃ - 남서풍 3m/s 45% -
13시 흐림 23℃ 22℃ - 남서풍 4m/s 40% -
14시 흐림 23℃ 22℃ - 서풍 4m/s 40% -
15시 맑음 23℃ 21℃ - 0% 서풍 4m/s 35% -
16시 맑음 23℃ 21℃ - 0% 남서풍 4m/s 35% -
17시 구름 많음 22℃ 21℃ - 20% 남서풍 3m/s 40% -
18시 구름 많음 21℃ 20℃ - 20% 남서풍 3m/s 45% -
19시 구름 많음 20℃ 20℃ - 20% 남서풍 2m/s 50% -
20시 구름 많음 18℃ 18℃ - 20% 남풍 2m/s 55% -
21시 구름 많음 18℃ 19℃ - 20% 서풍 1m/s 60% -
22시 구름 많음 18℃ 19℃ - 20% 북서풍 2m/s 60% -
23시 흐림 17℃ 18℃ - 30% 북서풍 2m/s 60% -
0시 흐림 17℃ 18℃ - 30% 북풍 2m/s 60% -
반응형
'Python > 웹크롤링' 카테고리의 다른 글
웹크롤링시 ConnectionResetError(104, 'Connection reset by peer') 해결방법 (0) | 2024.08.08 |
---|---|
파이썬 웹 크롤링(Web Crawling) 강좌 : 3. 네이버 베스트셀러 책이름, 저자, 가격 출력하기 (0) | 2023.10.21 |
파이썬 웹 크롤링(Web Crawling) 강좌 : 2. Yes24 특정 키워드 책 검색 순위 출력하기 (0) | 2023.10.21 |
파이썬 웹 크롤링(Web Crawling) 강좌 : 1. BeautifulSoup 간단 사용법 (0) | 2023.10.21 |