In this article i want to show How To Read Google RSS Feeds In Python, we are using feedparser for this article. so first of all you need to install Feed Parser. you can easily install feedparser using pip command, pip install feedparser.
Also you can check more Python articles in the below links
1: Complete PyQt5 GUI Development Course
2: Python TKinter GUI Development
OK now this is the complete code for How To Read Google RSS Feeds In Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import feedparser def parseRSS( rss_url ): return feedparser.parse( rss_url ) def getHeadlines(rss_url): headlines = [] feed = parseRSS(rss_url) for newsitem in feed['items']: headlines.append(newsitem['title']) return headlines allheadlines = [] newsurls = { 'googlenews': 'https://news.google.com/news/rss/?hl=en&ned=us&gl=US', } for key, url in newsurls.items(): allheadlines.extend(getHeadlines(url)) for hl in allheadlines: print(hl) |
So in the above code we have created a function to fetch the RSS feeds and return the parsed RSS
1 2 |
def parseRSS( rss_url ): return feedparser.parse( rss_url ) |
And this function is for grabbing RSS feed headlines and returns them as a list
1 2 3 4 5 6 7 8 |
def getHeadlines(rss_url): headlines = [] feed = parseRSS(rss_url) for newsitem in feed['items']: headlines.append(newsitem['title']) return headlines |
Also we need to create an empty list to hold all headlines
1 |
allheadlines = [] |
OK now this is the RSS Feeds that we want to fetch and combine, basically we are using Google News. but you can change this.
1 2 3 4 5 |
newsurls = { 'googlenews': 'https://news.google.com/news/rss/?hl=en&ned=us&gl=US', } |
In here we iterate over the feed urls and call the getHeadlines() to combine the returned headlines with allheadlines
1 2 3 |
for key, url in newsurls.items(): allheadlines.extend(getHeadlines(url)) |
And in here we iterate over the allheadlines list an print each headline
1 2 |
for hl in allheadlines: print(hl) |
So now run the complete code and this will be the result
Also you can watch the video for this article
Subscribe and Get Free Video Courses & Articles in your Email
sir can we customize to print 1 or 2 news feeds
if yes help to customize
yea you can customize
can you send the code please
please sir
import feedparser
index_number = 0
def parseRSS( rss_url ):
return feedparser.parse( rss_url )
def getHeadlines(rss_url):
headlines = []
feed = parseRSS(rss_url)
for newsitem in feed[‘items’]:
headlines.append(newsitem[‘title’])
return headlines
allheadlines = []
newsurls = {
‘googlenews’: ‘https://news.google.com/news/rss/?hl=en&ned=us&gl=IN’,
}
for key, url in newsurls.items():
allheadlines.extend(getHeadlines(url))
for hl in allheadlines:
index_number = index_number + 1
print(index_number , hl)
sir help how to customize please sir
hey i found a way to add index Numbers in the lines
i hope it will help you .
import feedparser
index_number = 0
def parseRSS( rss_url ):
return feedparser.parse( rss_url )
def getHeadlines(rss_url):
headlines = []
feed = parseRSS(rss_url)
for newsitem in feed[‘items’]:
headlines.append(newsitem[‘title’])
return headlines
allheadlines = []
newsurls = {
‘googlenews’: ‘https://news.google.com/news/rss/?hl=en&ned=us&gl=IN’,
}
for key, url in newsurls.items():
allheadlines.extend(getHeadlines(url))
for hl in allheadlines:
index_number = index_number + 1
print(index_number , hl)
Thank you sir, its help me a lot
import feedparser
GoogleFeed = feedparser.parse(“https://news.google.com/rss?hl=pt-BR&ned=br&gl=BR&gl=BR&ceid=BR:pt-419”)
for entry in GoogleFeed.entries:
titulo = entry.title
print (titulo)