I am trying to get news from the companies shown below, in my script. The problem is I am not getting any, even throughout the past month. Here is the code:
import requests
import json
import os
import datetime
from nltk.sentiment import SentimentIntensityAnalyzer
# Defining a dictionary of 'safe' companies and their Ticker Symbols:
safe_company_tickers = {"Berkshire Hathaway": "BRK.B",
"The Walt Disney Company": "DIS",
"Vanguard High-Dividend Yield ETF": "VYM",
"Procter & Gamble": "PG",
"Vanguard Real Estate Index Fund": "VNQ",
"Starbucks": "SBUX",
"Apple": "AAPL",
"Quest Diagnostics": "DGX",
"Microsoft": "MSFT"}
# Getting today's date only.
today = str(datetime.datetime.today()).split()[0]
#company_news = []
# Setting up URL with API key and company names
company_names = list(safe_company_tickers.keys())
url = ('https://newsapi.org/v2/everything?' +
'q=' + " OR ".join(company_names) + '&' +
'from=' + today + '&' +
'to=' + today + '&' +
'language=en&' +
'sortBy=publishedAt&' +
'pageSize=5&' +
'apiKey=my_api_key')
# Trying to send request to get news
try:
response = requests.get(url)
except Exception as exception:
with open(f"{os.getcwd()}\\trader_log.log", "a") as log:
log.write(f"[{datetime.datetime.now()}] Couldn't get news data. Error: {exception}.\n")
exit()
news = json.loads(response.text)
# Loop to read new articles, handle the data however you want, by using the tags 'title' or 'description', advisably.
for new in news["articles"]:
print(new)
# TODO: Make a for loop to loop through articles for each company listed.
# Initialize the sentiment analyzer
sia = SentimentIntensityAnalyzer()
# Analyze the sentiment of the input text. 'pos' is most likely what you should look at from the output of this command.
sentiment_scores = sia.polarity_scores(new["description"])
# Print the sentiment scores
print(sentiment_scores)
# Getting the company from the title, than getting its ticker symbol.
company = ""
for name in safe_company_tickers:
if name in new["description"]:
company = name
break
else:
with open(f"{os.getcwd()}\\trader_log.log", "a") as log:
log.write(f"[{datetime.datetime.now()}] Fetched article with none of the companies specified above.\n")
exit()
print(f"Company's Ticker: {safe_company_tickers[company]}")
The code above doesn't return any news for me. I haven't exceeded my daily quota. Weirdly enough, I can get articles with the query "Microsoft OR Apple OR Starbucks". I have checked the returns of join and the dates, and there seems to be no mistake. Thank you in advance!
Edit: Here are the responses for empty and non-empty requests:
Empty: {'status': 'ok', 'totalResults': 0, 'articles': []}
Non-Empty: {'status': 'ok', 'totalResults': 23155, 'articles': [{'source': {'id': None, 'name': 'Slashdot.org'}, 'author': 'feedfeeder', 'title': "TSMC Stock Rises on Bullish Chip Call. What It Means for Apple and Nvidia. - Barron's", 'description': "TSMC Stock Rises on Bullish Chip Call. What It Means for Apple and Nvidia.Barron's TSMC beats profit and revenue expectations in the fourth quarterCNBC TSMC Projects Return to Solid Growth as Tech RecoversBloomberg Television TSMC Thinks 2024 Will Repower Chi…", 'url': 'https://slashdot.org/firehose.pl?op=view&id=172774827', 'urlToImage': None, 'publishedAt': '2024-01-18T17:34:09Z', 'content': 'Sign up for the Slashdot newsletter! OR check out the new Slashdot job board to browse remote jobs or jobs in your areaDo you develop on GitHub? You can keep using GitHub but automatically sync your … [+268 chars]'}, {'source': {'id': None, 'name': 'Bringatrailer.com'}, 'author': 'bringatrailer', 'title': 'BaT Podcast Episode 52: Csaba Csere Shares Car and Driver Stories', 'description': 'Howard sits down with former Car and Driver Editor-in-Chief Csaba Csere (say it with us!) to talk about blouse shopping with David E. Davis, memorable overseas road tests, favorite racetrack […]<!-- BAT_SHORTCODE -->', 'url': 'https://bringatrailer.com/2024/01/18/bat-podcast-episode-52-csaba-csere-shares-car-and-driver-stories/', 'urlToImage': 'https://bringatrailer.com/wp-content/uploads/2024/01/BaT-podcast-art-rect-99049.png', 'publishedAt': '2024-01-18T17:32:14Z', 'content': 'Howard sits down with former Car and Driver Editor-in-Chief Csaba Csere (say it with us!) to talk about blouse shopping with David E. Davis, memorable overseas road tests, favorite racetrack experien… [+368 chars]'}, {'source': {'id': None, 'name': 'Biztoc.com'}, 'author': 'barrons.com', 'title': 'TSMC Stock Rises on Bullish Chip Call. What It Means for Apple and Nvidia', 'description': 'Taiwan Semiconductor Manufacturing said Thursday that it expects revenue growth this year to be at least 20%, more than double the rate of the wider chip market. That should be good news for its customers Nvidia and Apple, although TSMC cautioned on short-ter…', 'url': 'https://biztoc.com/x/466c1dd108e427dd', 'urlToImage': 'https://c.biztoc.com/p/466c1dd108e427dd/og.webp', 'publishedAt': '2024-01-18T17:32:09Z', 'content': 'Taiwan Semiconductor Manufacturing said Thursday that it expects revenue growth this year to be at least 20%, more than double the rate of the wider chip market. That should be good news for its cust… [+140 chars]'}, {'source': {'id': None, 'name': 'Biztoc.com'}, 'author': 'investors.com', 'title': 'Apple Stock Gains A New Bull On AI, Vision Pro Prospects', 'description': "stock rose Thursday after BofA Securities turned positive on the consumer electronics maker, citing artificial intelligence and the company's upcoming Vision Pro headset as sales drivers. BofA analyst Wamsi Mohan upgraded Apple stock to buy from neutral and r…", 'url': 'https://biztoc.com/x/4d6922f5c031ad93', 'urlToImage': 'https://c.biztoc.com/p/4d6922f5c031ad93/og.webp', 'publishedAt': '2024-01-18T17:32:09Z', 'content': "stock rose Thursday after BofA Securities turned positive on the consumer electronics maker, citing artificial intelligence and the company's upcoming Vision Pro headset as sales drivers.BofA analyst… [+307 chars]"}, {'source': {'id': None, 'name': 'Biztoc.com'}, 'author': 'investorplace.com', 'title': '3 Penny Stocks That Will Be the Surefire AI Winners of Tomorrow', 'description': 'These stocks offer speculative investors the benefits of AI with an attractive share price Artificial intelligence and penny stocks don’t necessarily go together like peanut butter and jelly, but there is some allure to AI penny stocks for risk-tolerant inves…', 'url': 'https://biztoc.com/x/b3273c42bc4b28f5', 'urlToImage': 'https://c.biztoc.com/p/b3273c42bc4b28f5/s.webp', 'publishedAt': '2024-01-18T17:30:17Z', 'content': 'These stocks offer speculative investors the benefits of AI with an attractive share priceArtificial intelligence and penny stocks dont necessarily go together like peanut butter and jelly, but there… [+306 chars]'}]}
{'source': {'id': None, 'name': 'Slashdot.org'}, 'author': 'feedfeeder', 'title': "TSMC Stock Rises on Bullish Chip Call. What It Means for Apple and Nvidia. - Barron's", 'description': "TSMC Stock Rises on Bullish Chip Call. What It Means for Apple and Nvidia.Barron's TSMC beats profit and revenue expectations in the fourth quarterCNBC TSMC Projects Return to Solid Growth as Tech RecoversBloomberg Television TSMC Thinks 2024 Will Repower Chi…", 'url': 'https://slashdot.org/firehose.pl?op=view&id=172774827', 'urlToImage': None, 'publishedAt': '2024-01-18T17:34:09Z', 'content': 'Sign up for the Slashdot newsletter! OR check out the new Slashdot job board to browse remote jobs or jobs in your areaDo you develop on GitHub? You can keep using GitHub but automatically sync your … [+268 chars]'}
Note: I got the non-empty response using "Apple" as a query, not the " OR ".join(...) or anything else, like I should. This is just for you to be able to see what a non empty response looks like.
Furthermore, escaping the & in Procter & Gamble using a backslash still doesnt work
Update: I tried playing around with the stocks listed, and it kind of turns out that Berkshire something was causing the problem. Removing or replacing this stock fixed the problem. But I don't know exactly why, can anyone help me on this?