네이트의 검색 키워드 가져오기
2025. 5. 28. 11:25ㆍPYTHON
반응형
네이트에서 검색어를 실시간으로 가져오기 위하여 아래와 같은 소스를 만들었다.
가져올때마다 검색어가 달라지는 양상은 나만 느끼는 것인가?
역시 실시간은 실시간인가 보다. 각자마다의 검색어는 다르니깐? 그런 것일까?
데이터베이스에 저장한들 의미가 없어보인다는..
기본적으로 selenium을 설치해야한다.
설치방법은 알고 있는 것으로 판단하고...
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import requests
from bs4 import BeautifulSoup
import time
class NateKeyWordMain:
def __init__(self):
print(f" _______________ INIT ________________")
self.url = "https://www.nate.com/?f=autorefresh"
# self.getNateKeyWord()
self.getNateKeyWord()
def getNateKeyWord(self):
try:
# 크롬 드라이버 경로 설정 (chromedriver가 시스템 PATH에 있어야 함)
options = Options()
options.add_argument('--headless') # 창을 띄우지 않음
options.add_argument('--no-sandbox') # 리눅스용
options.add_argument('--disable-dev-shm-usage') # 메모리 문제 방지
options.add_argument('--disable-gpu') # GPU 가속 비활성화
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36") # 실제 브라우저 UA 설정
driver = webdriver.Chrome(options=options)
driver.get(f"{self.url}")
time.sleep(3) # JS 렌더링 기다림
html = driver.page_source
# print(driver.page_source)
soup = BeautifulSoup(html, 'html.parser')
# print(soup)
# 예: 순위 키워드 추출
for li in soup.select(".kwd_list.type_biz ol li"):
num = li.select_one(".num").text
keyword = li.select_one("a").text
print(f"{keyword}")
driver.quit()
except Exception as e:
print(f" _________ ERROR getNateKeyWord _____ {e}")
if __name__ == "__main__":
NateKeyWordMain()
'PYTHON' 카테고리의 다른 글
FPDF란 무엇인가? (0) | 2025.05.09 |
---|---|
일단위 DB백업진행 (0) | 2025.04.09 |
📌 Doji(도지)란? (0) | 2025.04.01 |
✅ 서비스 중지 없이 MySQL 대용량 데이터(80GB) 동기화하는 방법 (1) | 2025.03.23 |
[PYTHON]asyncio: 파이썬 비동기 프로그래밍 완벽 가이드 (0) | 2025.02.27 |