Python: Get the top stories from Google news

Python Basic - 1: Exercise-8 with Solution

Write a Python program that retrieves the top stories from Google News.

Sample Solution:

Python Code :

# Import the 'bs4' module for web scraping and 'urlopen' function for opening URLs.
import bs4
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen

# URL of the news RSS feed
news_url = "https://news.google.com/news/rss"

# Open the URL using 'urlopen' and read the content of the webpage.
Client = urlopen(news_url)
xml_page = Client.read()

# Create a BeautifulSoup object to parse the XML content of the webpage.
soup_page = soup(xml_page, "xml")

# Find all XML tags with the name "item" to extract news items.
news_list = soup_page.findAll("item")

# Print news title, URL, and publish date for each news item.
for news in news_list:
    print("-" * 60)

Sample Output:

With Trump in China, Taiwan worries about becoming a 'bargaining chip'
Thu, 9 Nov 2017 11:19:13 GMT
Blue Dog Democrats taking hard line on GOP tax bill
Thu, 9 Nov 2017 11:01:58 GMT
A Year Later, The Shock Of Trump's Win Hasn't Totally Worn Off In Either Party
Thu, 9 Nov 2017 10:02:21 GMT
In a Texas town overwhelmed with grief, Pence delivers a message of support and faith
Thu, 9 Nov 2017 04:50:00 GMT
US, AT&T at odds over CNN in Time Warner deal
Thu, 9 Nov 2017 00:20:52 GMT
Self-driving bus crashes two hours after launch in Las Vegas
Thu, 9 Nov 2017 00:18:00 GMT
2017 CMA Awards Preach Unity, Crown Garth Brooks Entertainer of the Year
Thu, 9 Nov 2017 08:23:59 GMT
Spacey Cut From Film; Scenes To Be Re-Shot With Replacement Actor
Thu, 9 Nov 2017 09:57:00 GMT
FEMA to transport Puerto Rico's hurricane survivors to US mainland
Thu, 9 Nov 2017 10:48:23 GMT
Trump and Putin to meet on Friday in Vietnam
Thu, 9 Nov 2017 08:18:23 GMT
Syria war: Army retakes last IS urban stronghold
Thu, 9 Nov 2017 11:26:15 GMT
The Saudi Purge Isn't Just a Power Grab
Thu, 9 Nov 2017 09:15:34 GMT
Trump Tightens Cuba Embargo, Restricting Access to Hotels and Businesses
Wed, 8 Nov 2017 17:16:11 GMT
Battered by Trump, Obamacare triumphs at the polls
Wed, 8 Nov 2017 18:17:39 GMT
Terry Crews Confirms He Filed a Police Report After Making Sexual Assault Allegations
Thu, 9 Nov 2017 03:07:24 GMT
Former party chief Donna Brazile stokes divisions among Democrats
Thu, 9 Nov 2017 11:01:37 GMT
Theresa May faces fresh reshuffle after Priti Patel resignation
Thu, 9 Nov 2017 07:41:24 GMT
Barack Obama has to report to jury duty, too. (He didn't get picked.)
Wed, 8 Nov 2017 20:58:25 GMT
Report: Goodell “furious” about push for incentive-base deal
Thu, 9 Nov 2017 03:41:00 GMT
Danica Roem of Virginia to be first openly transgender person elected, seated in a US statehouse
Wed, 8 Nov 2017 19:37:57 GMT


The above Python code uses BeautifulSoup, a web scraping library, to fetch and parse an XML RSS feed of news articles. It extracts and prints the title, URL, and publication date for each news item in the feed.


Flowchart: Python - Get the top stories from Google news

Python Code Editor :


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to count the number of each character of a text file.
Next: Write a Python program to get a list of locally installed Python modules.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.