스크래핑 결과 시각화하기
- 기상청 날씨 정보 시각화해보기 - https://www.weather.go.kr/w/weather/forecast/short-term.do
- 필요한 라이브러리 불러오기
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
- 사이트에 요청 보내기
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://www.weather.go.kr/w/weather/forecast/short-term.do')
driver.implicitly_wait(1)
print(driver.find_element(By.ID, "my-tchart").text)
- 데이터 가공
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://www.weather.go.kr/w/weather/forecast/short-term.do')
driver.implicitly_wait(1)
temps = driver.find_element(By.ID, "my-tchart").text
temps = [int(i) for i in temps.replace("℃", "").split('\n')]
print(temps)
- lineplot을 이용해 시각화 해보기
import seaborn as sns
sns.lineplot(
x = [i for i in range(len(temps))],
y = temps
)
- ylim을 통해 범위 조절해보기
import matplotlib.pyplot as plt
plt.ylim(min(temps) - 2, max(temps) + 2)
sns.lineplot(
x = [i for i in range(len(temps))],
y = temps
)
해시코드 질문코드 빈도
- 해시코드 질문 태그의 빈도 확인
- 필요 라이브러리 불러오기
import requests
from bs4 import BeautifulSoup
user_agent = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"}
- 홈페이지 요청 보내고 필요한 내용 추출
import time
frequency = {}
for i in range(1, 11):
res = requests.get(f'https://hashcode.co.kr/?page={i}', user_agent)
soup = BeautifulSoup(res.text, 'html.parser')
ul_tags = soup.find_all('ul', 'question-tags')
for ul in ul_tags:
li_tags = ul.find_all('li')
for li in li_tags:
frequency[li.text.strip()] = frequency.get(li.text.strip(), 0) + 1
time.sleep(0.5)
print(frequency)
- 가장 많이 나온 단어 찾기
from collections import Counter
counter = Counter(frequency)
counter.most_common(10)
- seaborn으로 시각화하기
x = [i[0] for i in counter.most_common(10)]
y = [i[1] for i in counter.most_common(10)]
sns.barplot(x=x, y=y)
- 좀 더 보기좋게 만들어보기
plt.figure(figsize=(10, 10))
plt.title('Frequency of question in Hashcode')
plt.xlabel('Tag')
plt.ylabel('Frequency')
sns.barplot(x=x, y=y)
plt.show()
'프로그래머스 AI 데브코스 5기 > CS' 카테고리의 다른 글
스택(stack) (0) | 2023.03.27 |
---|---|
Git & Github (0) | 2023.03.27 |
web scrapping 기초 2 (0) | 2023.03.22 |
Web Scraping 기초 (0) | 2023.03.21 |
양방향 연결 리스트(Doubly Linked List) (0) | 2023.03.20 |