user6524857
user6524857

Reputation:

TypeError: 'set' object is not subscriptable

my flask application gives a 'set' error I have been coding a RSS feed web app but currently am having an error that I cant figure out; this is my code :

import feedparser

from flask import Flask
from flask import render_template

app = Flask(__name__)

RSS = {"http://feeds.bbci.co.uk/news/rss.xml",
   "http://rss.iol.io/iol/news", "http://feeds.foxnews.com/foxnews/latest", "http://rss.cnn.com/rss/edition.rss"}
#error occurs here

@app.route("/")
@app.route("/<publication>")
def get_news(publication="bbc"):
    #ERROR OCCURS HERE
    feed = feedparser.parse(RSS[publication])
    first_article = feed['entries'][0]

    return render_template("home.html",
                       title=first_article.get("title"),
                       published=first_article.get("publication"),
                       summary=first_article.get("summary"))


if __name__ == "__main__":
    app.run(debug=True, port=5000)

I get my error at these two lines

    feed = feedparser.parse(RSS[publication])
    first_article = feed['entries'][0]

can't figure out the actual error

Upvotes: 9

Views: 50278

Answers (2)

user2194299
user2194299

Reputation:

As Iron Fist comments, it seems that you are defining a set and using it as dictionary. It's difficult to be sure, but for what I see in the code, RSS should actually be a dictionary, using the name of the feeder as key. So:

RSS = {"bbc":"http://feeds.bbci.co.uk/news/rss.xml",
       "iol":"http://rss.iol.io/iol/news",
       "foxnews":"http://feeds.foxnews.com/foxnews/latest", 
       "cnn":"http://rss.cnn.com/rss/edition.rss"}

Upvotes: 2

Tagc
Tagc

Reputation: 9072

As Iron Fist points out, RSS is a set (which aren't subscriptable), although it looks as though you're trying to use it as a dictionary. Based on the default value you use for get_news, I'm hazarding a guess that you want something like this:

RSS = {"bbc": "http://feeds.bbci.co.uk/news/rss.xml",
       "iol": "http://rss.iol.io/iol/news",
       "fox": "http://feeds.foxnews.com/foxnews/latest",
       "cnn": "http://rss.cnn.com/rss/edition.rss"}

Upvotes: 11

Related Questions