<jupyter_start><jupyter_text>Instructor Do: Getting Data for Sentiment Analysis<jupyter_code># Initial imports
import pandas as pd
from dotenv import load_dotenv
import os
from newsapi import NewsApiClient


# Load env variables and read the News API Key enviroment variable
load_dotenv()
api_key = os.getenv("NEWSAPI")
# Create a newsapi client
newsapi = NewsApiClient(api_key=api_key)<jupyter_output><empty_output><jupyter_text>**NOTE:** The news changes constantly and the api is frequently refreshing. As such, the parameters in the next step might not return as many results as the example in the lesson plan.  If **no** results are returned, try setting the `q=` parameter to another news-worthy term, or removing the `country=` parameter to broaden the search.<jupyter_code># Fetch top news articles about oil in Canada 

oil_headlines = newsapi.get_top_headlines(q="gas", language="en", country="ca")

oil_headlines
# Print total articles
print(f"Total articles about oil: {oil_headlines['totalResults']}")

# Show sample article
oil_headlines["articles"][0]


# Transform the response dictionary to a DataFrame
oil_df = pd.DataFrame.from_dict(oil_headlines["articles"])

oil_df.head()


# Fetch all the news about Facebook Libra
libra_headlines = newsapi.get_everything(
    q="facebook libra", language="en", sort_by="relevancy"
)

# Print total articles
print(f"Total articles about Facebook Libra: {libra_headlines['totalResults']}")

# Show sample article
libra_headlines["articles"][0]


# Transform the response dictionary to a DataFrame
libra_df = pd.DataFrame.from_dict(libra_headlines["articles"])

libra_df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0914560770156438, 'fraction_numerical': 0.0012033694344163659, 'mean_word_length': 6.628440366972477, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '386098', 'license': 'MIT', 'n_tokens_mistral': 522, 'n_tokens_neox': 498, 'n_words': 168, 'path': 'PythonJupyterNotebooks/Week12-Day2-Activity3-sentiment_analysis_data.ipynb', 'repo_name': 'nomadic-me/CodingCheatSheets', 'size': '1662'}