copipe Python | 最近のニュースを取得し感情分析する。

概要

feedparserを使って最近のニュースのタイトルを取得し、SentimentIntensityAnalyzer()を使って感情分析し、感情がポジティブかネガティブかを数値化することができる。

コード

import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
nltk.download('vader_lexicon')
vader_analyzer = SentimentIntensityAnalyzer()

import feedparser
res = feedparser.parse("https://www.investing.com/rss/news_14.rss")
for x in res.entries:
    text = x.title
    result = vader_analyzer.polarity_scores(text)
    print(text + " => "+str(result["compound"]))

結果

Fed Decision, Brazil Rate Hike, Samsung Chip Warning - What's up in Markets => -0.34
World going through unprecedented chip shortage, China trade body says => -0.25
'Clean' investment funds less vulnerable to climate shocks, says EU watchdog => -0.4951
Analysis: Why Biden's securities regulator faces climate crackdown challenges => 0.3612
Some UK financial firms blame COVID for missing target of women in senior roles => -0.5574
New York stays top in finance as London loses ground, index shows => -0.128
Japan Inc offers lowest pay rises in 8 years as pandemic bites => -0.4588
Back to the '70s as Fed fuels boom and hopes for no Burns marks => 0.1531
Column: No real fight facing Fed so far - Mike Dolan => -0.5859
Japan households amass record financial assets as COVID-19 crimps spending => 0.1779