stock-market/news-sentiment-analysis

Ray Islam, PhD
Feb 14, 2023

--

Click here for the github link

#!/usr/bin/env python
# coding: utf-8
# Written by Dr. Ray Islam

#This code will analyze sentiment only from finance.yahoo.com/news/

# In[27]:
import requests
from bs4 import BeautifulSoup
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer

import nltk
nltk.download('vader_lexicon')


# In[28]:

# Get the html content from the website
url = "https://finance.yahoo.com/news/"
html_content = requests.get(url).content


# In[29]:

# Use BeautifulSoup to parse the html content
soup = BeautifulSoup(html_content, "html.parser")

# Find all the articles in the page
articles = soup.find_all("h3")

# Initialize the SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()


# In[30]:

# Loop through each article and get its text
for article in articles:
article_text = article.text
sentiment = sia.polarity_scores(article_text)["compound"]

# Print the article text and its sentiment
print("Article: ", article_text)
print("Sentiment: ", sentiment)
print("\n")

--

--

Ray Islam, PhD
Ray Islam, PhD

Written by Ray Islam, PhD

PhD in ML | AI Scientist | Professor | Author | Speaker | Reviewer: ICLR; RESS; JPHM | Member: AAAI | Marquis Who's Who | PhD | MASc | MSc | MBA | BSc. Eng.

No responses yet