I am trying to get a list of the top business/technology headlines using NewsApi in Python using this function:
def get_customized_news(phrase, n_news=15,pageAmount=30):
articles = newsapi.get_top_headlines(q=phrase,
language="en",
page_size = n_news,
page=pageAmount,
sources=NewsApiClient.get_sources(category='business,technology',language='en'))
print(articles['articles'])
However when doing so I would mostly get null results after typing simple phrases such as "Blackrock" or "Apple stock" and when replacing top headlines with the get_everything function I simply just get irrelevant results despite specifying the domains I want to use. How would I be able to get relevant, top headlines from pythons newsapi?
So it looks like the main problem you're having is with this line:
sources=NewsApiClient.get_sources(category='business,technology',language='en'))
self
(needs to be called as an instance method)sources
parameter needs to be a comma delimited list of only the source ids; for example sources="bbc-news,wired"
I ran the following with success:
from newsapi import NewsApiClient
from pprint import pprint
newsapi = NewsApiClient(api_key = SECRET_API_KEY)
def get_customized_news(phrase, category, n_news=15,pageAmount=1):
articles = newsapi.get_top_headlines(q=phrase,
language="en",
page_size = n_news,
page=pageAmount,
category=category)
pprint(articles['articles'])
get_customized_news("Apple", "business", 10, 1)
get_customized_news("Apple", "technology", 10, 1)