Search code examples
pythonnewsapi

How to get business and technology only news from NewsApi in python?


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?


Solution

  • So it looks like the main problem you're having is with this line: sources=NewsApiClient.get_sources(category='business,technology',language='en'))

    1. NewsApiClient.get_sources fails with a TypeError when I call it as its missing the parameter self (needs to be called as an instance method)
    2. As far as I can tell in the documentation, if you're using categories, you can only pass in a single category.
    3. Even if you fixed that call, get_sources returns a dictionary (or more likely JSON), which contains a list of dictionaries of metadata for all the sources, while the 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)