Iceloof / GoogleNews

Script for GoogleNews
https://pypi.org/project/GoogleNews/
MIT License
314 stars 88 forks source link

Fix get_news function with only period parameter specifies #143

Closed david-xhf closed 3 months ago

david-xhf commented 3 months ago

get_news() fetches results from news.google.com. While a user initiates a GoogleNews object with only 'period' specified (but not start & end dates specified), get_news() will still insert the start and end dates in the search keyword.

start = f'{self.__start[-4:]}-{self.__start[:2]}-{self.__start[3:5]}'
end = f'{self.__end[-4:]}-{self.__end[:2]}-{self.__end[3:5]}'
self.url = 'https://news.google.com/search?q={}+before:{}+after:{}&hl={}'.format(key, end, start, self.__lang.lower())

In this way, news.google.com will return empty or incomplete results. For example:

googlenews = GoogleNews()
googlenews = GoogleNews(lang='en', region='US')
googlenews = GoogleNews(period='1d')
googlenews.get_news('Dearborn')
print(self.url)
# https://news.google.com/search?q=Dearborn%20when%3A1d+before:--+after:--&hl=en

Trying this URL will show no results. without before and after in the keywords, the URL will be like: https://news.google.com/search?q=Dearborn+when:1d&hl=en-US&gl=US&ceid=US:en This will show correct list of results.