Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RSS Feed reader
#1
Hi,
I'm still new to Python and I'm playing around with an RSS reader.
The code actually works so far so I'm very proud of myself Big Grin

The code below reads ONE rss feed and matches it against keywords and based on that it perfoms an action.
The only thing is, it only reads ONE rss feed and I would like to let it read more than ONE feed.

So I thought that I could change:
d = feedparser.parse('https://feed1.rss')
into:
d = feedparser.parse('https://feed1.rss','https://feed2.rss')
But that doesn't seem to work.
What would be the easiest way to achieve this?


Original and full code:
#!/usr/bin/python
import feedparser
import os
import time

# Reset old_message
old_message = ""

# Reset flag
flag = 0

while True:
        # Whipe screen
        os.system('clear')
        # Read RSS Feed
        d = feedparser.parse('https://feed1.rss')

        # Gather title
        title = d['entries'][0]['title']
        # Title to uppercase
        title = title.upper()
        # Gather description
        description = d['entries'][0]['description']
        # Description to uppercase
        description = description.upper()

        # Combine title and description to message
        message = title + " " + description

        # Conditions need to be in capitals
        if message.find("56") != -1:
                 flag = 1

        if message.find("HO") != -1:
                flag = 1

        # If flag = true
        if flag == 1:
                if message != old_message:
                        print("\033[1;31;40m *** ALERT ***")
                        old_message = message

        print title
        print description
        print flag
        flag = 0
        print("\033[1;32;40m")

        time.sleep(10)
Reply
#2
I am not positive but you could try something like:

a = feedparser.parse('https://feed1.rss')
b = feedparser.parse('https://feed2.rss')
c = feedparser.parse('https://feed3.rss')

title_a = a['entries'][0]['title']
title_b = b['entries'][0]['title']
title_c = c['entries'][0]['title']
Otherwise, you might look into something called threading.
Reply
#3
(Aug-03-2018, 03:52 PM)Vysero Wrote: I am not positive but you could try something like:

a = feedparser.parse('https://feed1.rss')
b = feedparser.parse('https://feed2.rss')
c = feedparser.parse('https://feed3.rss')

title_a = a['entries'][0]['title']
title_b = b['entries'][0]['title']
title_c = c['entries'][0]['title']

I don't think this will work since you're splitting the entries per feed.
I want to combine the feeds in one variable (d)
Reply
#4
That's okay I am complexly inexperienced with the feedparser class. You might try something like this:

RSS_URLS = [
    'https://feed1.rss',
    'https://feed2.rss',
    ]

feeds = []
for url in RSS_URLS:
    feeds.append(feedparser.parse(url))

for feed in feeds:
    for post in feed.entries:
        print(post.title) #or whatever logic you need to use
Reply
#5
(Aug-03-2018, 07:23 PM)Vysero Wrote: That's okay I am complexly inexperienced with the feedparser class. You might try something like this:

RSS_URLS = [
    'https://feed1.rss',
    'https://feed2.rss',
    ]

feeds = []
for url in RSS_URLS:
    feeds.append(feedparser.parse(url))

for feed in feeds:
    for post in feed.entries:
        print(post.title) #or whatever logic you need to use

This prints all RSS items, I just want the last one.
For post in feed.entries = 1 to 1 ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Implementing classes , RSS feed program Miraclefruit 1 3,496 Oct-16-2017, 04:11 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020