Using RSS in Python Program

#1. install feedparser library

In our headlines.py file, we’ll make modifications to import the feedparser library we installed, parse the feed, and grab the first article. We’ll build up HTML formatting around the first article and show this in our application. If you’re not familiar with HTML, it stands for Hyper Text Markup Language and is used to define the look and layout of text in web pages. It’s pretty straightforward, but if it’s completely new to you, you should take a moment now to go through a beginner tutorial to get familiar with its most basic usage. There are many free tutorials online, and a quick search should bring up dozens. A popular and very beginner-friendly one can be found at http://www.w3schools.com/html/

Our new code adds the import for the new library, defines a new global variable for the RSS feed URL, and further adds a few lines of logic to parse the feed, grab the data we’re interested in, and insert this into some very basic HTML. It looks similar to this:

#2. write the following code.

The first line of this function passes the BBC feed URL to our feedparser library, which downloads the feed, parses it, and returns a Python dictionary. In the second line, we grabbed just the first article from the feed and assigned it to a variable. The entries entry in the dictionary returned by feedparser contains a list of all the items that include the news stories we spoke about earlier, so we took the first one of these and got the headline or title, the date or the published field, and the summary of the article (that is, summary) from this. In the return statement, we built a basic HTML page all within a single triple-quoted Python string, which includes the and tags that all HTML pages have as well as an
Heading that describes what our page is;

which is a bold tag that shows the news headline; which stands for the italics tag that shows the date of the article; and which is a paragraph tag to show the summary of the article. As nearly all items in an RSS feed are optional, we used the python.get() operator instead of using index notation (square brackets), meaning that if any information is missing, it’ll simply be omitted from our final HTML rather than causing a runtime error.

#3. go to the file where you save and open it on command line

e.i. cd C:\Users\dell\Desktop\firstapp

#4. Write your file name with .py and press wnter.

#5. Open your Browser.

#6. enter the in the url. and press enter

Get the source code on the Git hub

Share This Post To Your Friends

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *