Python Tutorial: How to Parse and Combine RSS News headlines using feedparser

Python Tutorial: How to Parse and Combine RSS News headlines using feedparser

This python code will take a list of RSS newsfeed urls, fetch and combine all the news headlines into one list.  The feedparser package is required, install it with the following command.

Example Output

Download this code as a IPython Notebook

I hope you find this python example useful and educational. You are free to use the above code how you see fit. I do however suggest that you implement some type of rss feed caching as some services may block your ip for excessive requests.

More Python Goodness

14 Replies to “Python Tutorial: How to Parse and Combine RSS News headlines using feedparser”

    1. with open(“myfile.csv”,’w’) as f:
      for hl in allheadlines:
      print(hl)
      f.writelines(hl+”n”)

      Get the indends right, and I say changed csv to txt as some headlines have commas in them

    1. Line 13 just appends the title to the array. Instead, append entire newsitem instead of newsitem[“title”]

      Then around line 34-35 you would output the HTML link using hl[“title”] and hl[“link”]

  1. Thanks a lot. Is there any way to get image link from this? I’m trying to figure out but no luck. Good job on the code.

    1. You can get at the newsitem’s image (if it has one) inside the getHeadlines() function. Since newsfeed items don’t always have images the following code snippet is the easiest solution without raising a key error.

Leave a Reply

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