본문 바로가기
E | ngineering

Seaborn 활용 1

by 덞웖이 2024. 10. 4.

환경

전 포스팅 참조

사전 세팅

전 포스팅 참조

    # 한글이 포함된 데이터 사용 시
    sudo apt install msttcorefonts
    # 터미널 크기가 13줄 이상이어야 dialog가 나타나므로 주의

패키지

전 포스팅 참조

    import seaborn as sns
    from matplotlib import pyplot as plt
    from matplotlib import font_manager as fm

실행

    # 크롤
    # 네이버 날씨
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrm_options)
    url = 'https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=0&ie=utf8&query=날씨'
    driver.get(url)
    driver.implicitly_wait(2)

    # 날씨 부분 타겟 
    target_lis = driver.find_elements(By.CSS_SELECTOR, '.graph_inner._hourly_weather > ul > li')
    time, temp = [], []
    for e in target_lis:
        # 하위 태그 셀렉트, 텍스트 수집
        target = e.find_element(By.CSS_SELECTOR, 'dt.time > em').text
        # 이상한거 버려
        if target != '':
            time.append(e.find_element(By.CSS_SELECTOR, 'dt.time > em').text)
            temp.append(int(e.find_element(By.CSS_SELECTOR, 'dd.degree_point span.num').text[:-2]))
    driver.quit()
    # 시각화
    # 폰트 찾을때 사용
    for font in fm.findSystemFonts(): print(font)

    # matplotlib 캐시 글리치 생기면 사용
    %rm ~/.cache/matplotlib -rf

    # 폰트 설정
    font_path = './NanumGothicCoding.ttf' # 그냥 프로젝트 폴더로 가져옴
    font_prop = fm.FontProperties(fname=font_path)
    plt.rcParams['font.family'] = font_prop.get_name()

    # Plot setup: 순서 중요함
    plt.figure(figsize=(20, 6)) # this goes before sns setup
    sns.lineplot(x=time, y=temp)
    plt.title('Hourly weather')
    plt.xlabel('Time')
    plt.ylabel('Temperature')
    plt.show()

한글까지 정상 출력 확인

'E | ngineering' 카테고리의 다른 글

WordCloud 활용  (0) 2024.10.05
Seaborn 활용 2  (0) 2024.10.04
Selenium으로 Crawl  (0) 2024.10.04
BS4만으로 Crawl  (0) 2024.10.03
자료구조/알고리즘  (0) 2024.10.01