Notice
Recent Posts
Recent Comments
Link
터칭 데이터
스크래핑 결과 시각화 - 기상청 데이터 본문
In [1]:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
4-2. 스크래핑 결과 시각화하기 I - 기상청 날씨 정보 조회¶
- Selenium과 Seaborn을 이용해서 날씨 정보를 가져온 후 Lineplot을 그려봅시다.
Target: 기상청 날씨 스크래핑하기¶
다음 사이트에 Selenium을 이용해서 날씨 정보를 가져와봅시다. : https://www.weather.go.kr/w/weather/forecast/short-term.do
In [2]:
# 스크래핑에 필요한 라이브러리를 불러와봅시다.
from selenium import webdriver
from selenium.webdriver import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver import Keys, ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
지난 실습에서 다룬 마우스 실습을 통해 로그인 창에 접속하는 것에 성공했는데요,
여기에 이제 키보드 입력을 넣어서 로그인을 완료해봅시다.
In [27]:
# driver를 이용해 기상청 날씨 데이터를 가져와봅시다.
import time
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.maximize_window()
driver.get("https://www.weather.go.kr/w/weather/forecast/short-term.do")
# driver.implicitly_wait(10)
time.sleep(10)
res = driver.find_elements(By.XPATH, '//*[@id="digital-forecast"]/div[1]/div[3]/div[2]/div[1]/div[6]')
print(len(res))
cels = res[0].get_attribute('data-data')
cels = cels.strip('[').strip(']').split(',')
1
In [34]:
cels = list(map(int, cels))
print(len(cels))
print(cels)
84
[21, 20, 20, 19, 18, 18, 16, 16, 15, 15, 14, 13, 12, 11, 10, 9, 9, 8, 8, 9, 10, 12, 14, 16, 17, 18, 18, 18, 17, 16, 14, 13, 12, 11, 11, 10, 9, 8, 8, 7, 7, 7, 7, 9, 11, 14, 16, 17, 18, 18, 18, 18, 16, 15, 13, 12, 11, 11, 10, 9, 9, 9, 9, 8, 8, 8, 8, 9, 12, 15, 16, 17, 18, 19, 19, 18, 17, 15, 13, 13, 12, 11, 10, 10]
꺾은선 그래프(Line Plot) 을 이용해 앞으로의 기온의 추이를 나타내봅시다.
In [32]:
# 받아온 데이터를 통해 꺾은선 그래프를 그려봅시다.
# x = Elapsed Time(0~len(temperatures)
# y = temperatures
import seaborn as sns
sns.lineplot(
x = [i for i in range(len(cels))],
y = cels
)
Out[32]:
<Axes: >
In [36]:
# 받아온 데이터의 ylim을 조금 더 길게 잡아봅시다.
import matplotlib.pyplot as plt
plt.ylim(min(cels) - 2, max(cels) + 2)
plt.title("Expected Temperature from now on")
sns.lineplot(
x = [i for i in range(len(cels))],
y = cels
)
Out[36]:
(5.0, 23.0)
Out[36]:
Text(0.5, 1.0, 'Expected Temperature from now on')
Out[36]:
<Axes: title={'center': 'Expected Temperature from now on'}>
In [ ]:
# 받아온 데이터를 통해 꺾은선 그래프를 그려봅시다.
'데이터 시각화(Data Visualization)' 카테고리의 다른 글
스크래핑 결과 시각화 - 해시코드 + 워드클라우드 (0) | 2023.10.27 |
---|---|
워드클라우드(Wordcloud) (0) | 2023.10.27 |
스크래핑 결과 시각화 - Hashcode (0) | 2023.10.27 |
시각화 라이브러리 Seaborn (0) | 2023.10.27 |