
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
저는 감성 분석을 해서 작동이 안됩니다. google colab 에서요
from bs4 import BeautifulSoup
import requests
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
# 검색할 키워드
query = "samsung electronics"
# 네이버 뉴스 검색 결과 URL
url = f"https://search.naver.com/search.naver?query={query}&where=news&ie=utf8&sm=nws_hty"
# 뉴스 헤드라인을 저장할 리스트
headlines = []
# 네이버 뉴스 검색 결과 페이지에서 뉴스 헤드라인 스크래핑
for page in range(1, 6): # 1페이지부터 5페이지까지 검색 결과 수집
res = requests.get(f"{url}&start={page*10-9}")
soup = BeautifulSoup(res.text, "html.parser")
titles = soup.find_all("a", class_="_sp_each_title")
for title in titles:
headlines.append(title.text)
# VADER 분석기 생성
analyzer = SentimentIntensityAnalyzer()
# 뉴스 헤드라인 감성분석 수행
for headline in headlines:
vs = analyzer.polarity_scores(headline)
print(headline)
print(f"Sentiment: {vs['compound']:.2f}")
print("="*50)
