A simple Python script that fetches the latest U.S. news headlines and performs a quick sentiment check using lists of positive and negative words. Perfect for a lightweight peek into how “positive” or “negative” today’s headlines sound!
- Live Headlines: Fetches current top headlines from News API
- Word-Based Sentiment: Counts how many positive and negative words appear in each headline
- Readable Output: Prints each headline with its sentiment word counts
- Lightweight: Uses only Python’s standard libraries +
requests
-
Make sure you have Python 3.x installed.
-
Install the required dependency:
pip install requests
-
Get your free API key from newsapi.org
-
Set the API key as an environment variable:
macOS / Linux:
export NEWS_API_KEY="your_api_key_here”(to make it permanent, add this line to your ~/.bashrc or ~/.zshrc)
Windows (PowerShell):
setx NEWS_API_KEY "your_api_key_here"├── #1024_mini_news_headline_analyzer.py
├── positive-words.txt
└── negative-words.txt
Run the script:
python #1024_mini_news_headline_analyzer.pyExample Output:
'Markets surge as tech stocks rebound' → {'num_pos_words': 2, 'num_neg_words': 0}
'Wildfires cause major damage across the west' → {'num_pos_words': 0, 'num_neg_words': 1}- Fetches top U.S. headlines from the News API
- Loads positive and negative words from the text files
- Counts how many occur in each headline
- Prints a quick sentiment summary for each one
MIT License — free to use, modify, and share.