Extracting News From Google Using Python

The situation, wherein you are running out of time but still do not want to miss your dose of daily news. Then this article is for you.

When it comes to reading news, not everyone is interested in every topic. Few may be interested in sports, others may be interested in either politics or spiritual news, or something else. So, based on individual interest, one would like to go and spend time reading.

In this article, I’ll guide you on how you can create your own Python utility to extract top headlines from google news based on the user’s interest. User can provide topic of their choice and links of top headlines would be displayed.

Import Required Packages

In order to start with Python, the very first demand is to grab all the required packages. Here we need two packages, one for extracting news and another one for handling data. Let’s go ahead and install both the packages:

pip install GoogleNews
pip install pandas

Once these are installed, one can go ahead and import them as shown below:

import from GoogleNews import GoogleNews
import pandas as pd

Extract News

Let’s write the code to extract the top news related to Russia which was published a day back.

news = GoogleNews(period=’1d’)
news.search(“Russia”)
result = news.result()
data = pd.DataFrame.from_dict(result)
data = data.drop(columns=[“img”])
data.head()

Display Output

Now, that we have extracted the data from google, next we will go ahead and display that data as shown below:

for res in result:
  print(“Title : “,res[“title”])
  print(“News : “,res[“desc”])
  print(“Detailed news : “,res[“link”])

On executing all the above lines, you would get output something similar to this:

Now, you can click on the hyperlink of your choice and it will redirect you to the news. Hope you enjoyed reading this article.

You can also watch the demonstration of this article on my YouTube channel by following this link.


Similar Articles