커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
2-7 감성분석 관련 질문
[왕초보] ChatGPT 300% 활용하기 v0
2주차
북마크
최*연
댓글
5
추천
0
조회수
13
조회수
13
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


강의 그대로 chatgpt에 질문해 나온 코드로 입력했는데 결과값이 '검색어 'Samsung Electronics'에 대한 네이버 뉴스 헤드라인 감성 분석 결과:'로 나옵니다.

그래서 이상한거 같아 올려주신 자료에서 있는 최종코드로 입력했는데 결과값이 아얘 안나옵니다.


스파르타 즉문즉답


작성한 코드 및 에러 메세지


chatgpt에 질문해서 나온 코드

import requests
from bs4 import BeautifulSoup
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer


# 검색어 설정
keyword = "Samsung Electronics"


# 네이버 뉴스 검색 URL
url = f"https://search.naver.com/search.naver?where=news&query={keyword}&sm=tab_opt&sort=0&photo=0&field=0&reporter_article=&pd=3&ds=&de=&docid=&nso=so%3Ar%2Cp%3Afrom20230101to20240101%2Ca%3Aall&mynews=0&refresh_start=0&related=0"


# HTTP GET 요청 보내기
response = requests.get(url)
response.raise_for_status()


# HTML 파싱
soup = BeautifulSoup(response.text, 'html.parser')


# 뉴스 헤드라인 가져오기
headlines = []
for headline_tag in soup.select('.news .type01 li dt a[title]'):
    headlines.append(headline_tag['title'])


# Vader 감성 분석기 초기화
analyzer = SentimentIntensityAnalyzer()


# 감성 분석 및 결과 출력
print(f"검색어 '{keyword}'에 대한 네이버 뉴스 헤드라인 감성 분석 결과:\n")
for headline in headlines:
    sentiment_score = analyzer.polarity_scores(headline)['compound']
    if sentiment_score >= 0.05:
        sentiment = "Positive"
    elif sentiment_score <= -0.05:
        sentiment = "Negative"
    else:
        sentiment = "Neutral"
    
    print(f"Headline: {headline}")
    print(f"Sentiment: {sentiment}")
    print()



올려주신 자료 코드

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)



취소
 공유
취소
댓글 0
댓글 알림
나의얼굴