mattlisiv / newsapi-python

A Python Client for News API
MIT License
375 stars 130 forks source link

Error regarding Newsapi-python get_top_headlines() #77

Closed ken1009us closed 1 year ago

ken1009us commented 1 year ago

Describe the bug

When I used only sources for the given parameter, I was able to retrieve the data. However, if I used q and country or other parameters, I received 0 results. For example:

This works:

top_headlines = newsapi.get_top_headlines(sources='bbc-news, the-verge')
{'status': 'ok', 'totalResults': 26, 'articles': .......}

But this doesn't work:

top_headlines = newsapi.get_top_headlines()
top_headlines = newsapi.get_top_headlines(q='tesla', country='us')
top_headlines = newsapi.get_top_headlines(category='business')
{'status': 'ok', 'totalResults': 0, 'articles': []}

I also tried:

top_headlines = newsapi.get_top_headlines(language='en-US')

but got this error:

top_headlines = newsapi.get_top_headlines(language='en-US')
  File "myproject/venv/lib/python3.8/site-packages/newsapi/newsapi_client.py", line 120, in get_top_headlines
    raise ValueError("invalid language")
ValueError: invalid language

I replaced the en in newsapi.const.language with en-US and I can receive the data right now but the source of the data is only google-news.

Also, if I used other countries, for example:

top_headlines = newsapi.get_top_headlines(country='jp')

I received 0 results.

mattlisiv commented 1 year ago

@ken1009us , please use the latest package 0.2.7 that was published this morning. It should address the error invalid language and allow you to proceed.

ken1009us commented 1 year ago

Thank you for your reply.

I have already installed the latest package 0.2.7 but I think I still got the same issue.

I need to go to the site-packages.newsapi.newsapi_client.py to replace the language="en" with language="en-US". This action can let me grab the data for api.get_top_headlines(), api.get_top_headlines(country="us") and api.get_top_headlines("uk").

However, it didn't fix the error regarding using other given parameters. For example, if I change the country to other values. I still got 0 results.

Also, google-news is the only resource for the received article. There are no other resources if I used api.get_top_headlines().

$ pip freeze | grep newsapi
newsapi-python==0.2.7
(base)
mattlisiv commented 1 year ago

Please see reply here: https://github.com/mattlisiv/newsapi-python/issues/75#issuecomment-1451856918

The scarce results are an issue you will need address with the NewsAPI team.