Build an Application to extract news from Google News Feed Using Python

Prerequisite Python tkinter

In this article, we are going to write a python script to extract news articles from Google News Feed by using gnewsclient module and bind it with a GUI application. gnewsclient is a python client for Google News Feed. This API has to installed explicitly first in order to be used.

Installation

The following terminal command installs gnewsclient package along with all its required libraries. So, one just simply has to run this command in their terminal.

pip install gnewsclient

Using the module

  1. Import the gnewsclient module
  2. Create a NewsClient object and set current parameter settings
  3. Get news feed

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import module
from gnewsclient import gnewsclient
  
# declare a NewsClient object 
client = gnewsclient.NewsClient(language='hindi', location='india', topic='Business', max_results=5)
  
# get news feed
client.get_news()

chevron_right


Output:



Following code depicts how other factors like location, language and topic can be printed from information collected by this module:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import gnewsclient
from gnewsclient import gnewsclient
  
client = gnewsclient.NewsClient(language='hindi'
                                location='india'
                                topic='Business',
                                max_results=5)
  
# prints location
print("Location: \n",client.locations)
print()
  
# prints languages
print("Language \n",client.languages)
print()
  
# prints topics
print("Topic \n",client.topics)

chevron_right


Output:

Example:

Program 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from gnewsclient import gnewsclient
  
client = gnewsclient.NewsClient(language='english'
                                location='india'
                                topic='sports'
                                max_results=3)
  
news_list = client.get_news()
  
for item in news_list:
    print("Title : ", item['title'])
    print("Link : ", item['link'])
    print("")

chevron_right


Output:



Program 2: This code implements the methodology of program 1 in GUI.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import modules
from tkinter import *
from gnewsclient import gnewsclient
  
# defined funtions
def news():
    client = gnewsclient.NewsClient(
        language=lang.get(), location=loc.get(), topic=top.get(), max_results=3)
    news_list = client.get_news()
    result_title.set(news_list[0]["title"] + "\n" +
                     news_list[1]["title"] + "\n" + news_list[2]["title"])
  
  
# tkinter object
master = Tk()
master.title("NEWS")
  
# background set to grey
master.configure(bg='light grey')
  
# Variable Classes in tkinter
result_title = StringVar()
result_link = StringVar()
  
# Creating label for each information
# name using widget Label
Label(master, text="Choose language :", bg="light grey").grid(row=0, sticky=W)
Label(master, text="Choose Location :", bg="light grey").grid(row=1, sticky=W)
Label(master, text="Choose Topic :", bg="light grey").grid(row=2, sticky=W)
  
  
lang = Entry(master)
lang.grid(row=0, column=1)
  
loc = Entry(master)
loc.grid(row=1, column=1)
  
top = Entry(master)
top.grid(row=2, column=1)
  
  
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=result_title,
      bg="light grey").grid(row=3, column=1, sticky=W)
  
# creating a button using the widget
# Button to call the submit function
Button(master, text="SHOW", command=news, bg="white").grid(row=1, column=3)
  
  
mainloop()

chevron_right


Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.