In this post, you will learn about how to use GoogleNews search Python library to get or retrieve or scrape news from Google News for last N number of days. This would be very helpful for someone wanting to track new work / projects in relation to machine learning, data science, deep learning or any field including sports, politics etc. Without further ado, lets jump in right away. You can log into Google colab and practise the code.
Step 1: First and foremost, lets install GoogleNews python library.
pip install GoogleNews
Step 2: Instantiate GoogleNews object. One can pass the language and period to instantiate the object. The parameter, period, represents the news from the last N number of days. One can represent the last 3 days as ‘3d’ or yesterday as ‘1d’
from GoogleNews import GoogleNews ''' Language: lang as English Period: period as number, N, representing news from last N days ''' googlenews = GoogleNews(lang='en', period='1d')
Step 3: Get the news of the last few days based on the search text. Method, search, is used to get the result as a list of JSON objects. For storing results, the result method is invoked on the GoogleNews object. The parameter sort is passed TRUE to sort the result with the first result as most latest one. Note clear method which is used to clear GoogleNews object such that fresh search results get stored in the results object.
Step 4: Iterate through result objects to print title, description, and URL for each news.
''' Print the title, description and URL of the news ''' for result in results: print('\n\nTITLE:', result['title'], '\nDESC:', result['desc'], '\nURL: ', result['link'])