Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a simple RSS reader in Python 3.7?

I built a simple RSS reader on Python and it is not working. In addition, I want to get the featured image source link of every post and I didn't find a way to do so.

it shows me the Error: Traceback (most recent call last): File "RSS_reader.py", line 7, in feed_title = feed['feed']['title']

If there are some other RSS feeds that work fine. So I don't understand why there are some RSS feeds that are working and others that aren't

So I would like to understand why the code doesn't work and also how to get the featured image source link of a post I attached the code, is written on Python 3.7

import feedparser
import webbrowser

feed = feedparser.parse("https://finance.yahoo.com/rss/")

feed_title = feed['feed']['title']
feed_entries = feed.entries

for entry in feed.entries:

    article_title = entry.title
    article_link = entry.link
    article_published_at = entry.published # Unicode string
    article_published_at_parsed = entry.published_parsed # Time object
    article_author = entry.author
    content = entry.summary
    article_tags = entry.tags


    print ("{}[{}]".format(article_title, article_link))
    print ("Published at {}".format(article_published_at))
    print ("Published by {}".format(article_author))
    print("Content {}".format(content))
    print("catagory{}".format(article_tags))
like image 232
Kadio Avatar asked May 01 '19 12:05

Kadio


People also ask

What is Feed Parser in Python?

Universal Feed Parser is a Python module for downloading and parsing syndicated feeds. It can handle RSS 0.90, Netscape RSS 0.91, Userland RSS 0.91, RSS 0.92, RSS 0.93, RSS 0.94, RSS 1.0, RSS 2.0, Atom 0.3, Atom 1.0, and CDF feeds.


3 Answers

You can also use xml parser libraries like beatifulsoup (https://www.crummy.com/software/BeautifulSoup/bs4/doc/) and create custom parsers. A sample customer parser code can be found here (https://github.com/vintageplayer/RSS-Parser). A walk through the same can read here (https://towardsdatascience.com/rss-feed-parser-in-python-553b1857055c)

Though libraries can be useful, beautifulsoup is an extremely handy library to try out.

like image 63
Aditya Agarwal Avatar answered Oct 25 '22 09:10

Aditya Agarwal


A few things.

1) First feed['feed']['title'] does not exist.
2) At least for this site entry.author, entry.tags do not exist
3) It seems feedparser is not compatible with python3.7 (it gives me KeyError, "object doesn't have key 'category')

So as a starting point try to run the following code in python 3.6 and go from there.

import feedparser
import webbrowser

feed = feedparser.parse("https://finance.yahoo.com/rss/")

# feed_title = feed['feed']['title']  # NOT VALID
feed_entries = feed.entries

for entry in feed.entries:

    article_title = entry.title
    article_link = entry.link
    article_published_at = entry.published # Unicode string
    article_published_at_parsed = entry.published_parsed # Time object
    # article_author = entry.author  DOES NOT EXIST
    content = entry.summary
    # article_tags = entry.tags  DOES NOT EXIST


    print ("{}[{}]".format(article_title, article_link))
    print ("Published at {}".format(article_published_at))
    # print ("Published by {}".format(article_author)) 
    print("Content {}".format(content))
    # print("catagory{}".format(article_tags))

Good luck.

like image 9
Bruno Vermeulen Avatar answered Oct 25 '22 08:10

Bruno Vermeulen


I have used BeautifulSoup for a beginner RSS feed reader project (You need to install lxml for it to work since we are dealing with xml):

from bs4 import BeautifulSoup
import requests
url = requests.get('https://realpython.com/atom.xml')

soup = BeautifulSoup(url.content, 'xml')
entries = soup.find_all('entry')

for i in entries:
  title = i.title.text
  link = i.link['href']
  summary = i.summary.text
  print(f'Title: {title}\n\nSummary: {summary}\n\nLink: {link}\n\n------------------------\n') 

You can find the Youtube video here: https://www.youtube.com/watch?v=8HbqO-TfjlI

like image 2
Vahid Avatar answered Oct 25 '22 08:10

Vahid