플그래밍/파이써언

[파이썬] Selenium - FnGuide 오늘의 급상승 검색어 추출하기

훗티v 2024. 2. 3. 14:17
728x90
반응형

이번에는 FnGuide사이트에 게시되는 오늘의 급상승 검색어를 추출해볼께요

 

FnGuide

 

 

아래 키워드 클라우드 부분을 추출할 예정이에요

추출하면서 단어 크기에 따른 비중도 함께 추출해볼께요

 

 

사이트에 접속하면 공지사항 팝업이 뜨는데 이것부터 닫아줄께요

팝업 닫기 버튼을 찾아서 클릭해줍니다

button = driver.find_element(By.ID, "closeBtn2")

button.click()

 

 

그 다음에는 워드클라우드를 찾아서 키워드를 추출하고

class를 활용하여 각 키워드의 비중 또한 추출합니다

cloud_div = driver.find_element(By.ID, "cloud")

span_tags = cloud_div.find_elements(By.TAG_NAME, "span")

for span in span_tags:
    text_content = span.text
    class_names = span.get_attribute("class")
    print(f"{text_content} (비중: {class_names.split(' ')[0][1:]})")

 

 

이렇게하면 FnGuide 메인페이지의 오늘의 급상승 키워드 추출이 가능합니다

 

전체코드

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from subprocess import CREATE_NO_WINDOW
import time

url = 'https://www.fnguide.com'

option = Options()
option.add_argument('--disable-gpu')
option.add_argument('--window-size=1920x1080')
option.add_argument('--start-maximized')
option.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36')

service = Service()
service.creation_flags = CREATE_NO_WINDOW
driver = uc.Chrome(service=service, options=option)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get(url)

button = driver.find_element(By.ID, "closeBtn2")

button.click()

cloud_div = driver.find_element(By.ID, "cloud")

span_tags = cloud_div.find_elements(By.TAG_NAME, "span")

for span in span_tags:
    text_content = span.text
    class_names = span.get_attribute("class")
    print(f"{text_content} (비중: {class_names.split(' ')[0][1:]})")

 

질문은 댓글에 남겨주세요~

반응형