-1

I wrote a small simple program which checks the RSS feed of a german news site, saves the pubDate, title, description and link of the last article and saves it to a txt file. After running over 11 hours on my raspberry pi I suddenly get a ValueError:

Time: [10:49:25] - Iteration nr. 2588 - Articles found: 21
Traceback (most recent call last):
  File "/home/thore/Desktop/main.py", line 16, in <module>
    if f.entries[0].title != last_item or last_item == "":
IndexError: list index out of range

Why is that happening?

my Code:

import feedparser
import datetime
import time

last_item = ("")
x = 1
news_nr = 1
articles_found = 0

while True:
    todays_date = datetime.date.today()
    today_formatted = todays_date.strftime("%d.%m.%Y")
    time_now = datetime.datetime.now()
    time_formatted = time_now.strftime("%H:%M:%S")
    f = feedparser.parse("https://www.tagesschau.de/xml/rss2/")
    if f.entries[0].title != last_item or last_item == "":
        with open("Tagesschau.txt", "a") as myfile:
            myfile.write(f"""
            Article nr.: {news_nr} - Saved at: Date {today_formatted}, Time {time_formatted}\n
            Published: {f.entries[0].published}
            Title: {f.entries[0].title}
            Text: {f.entries[0].description}
            Link: {f.entries[0].link}\n   
            """)
        myfile.close()
        news_nr += 1
        articles_found += 1
        last_item = f.entries[0].title
    print(f"Time: [{time_formatted}] - Iteration nr. {x} - Articles found: {articles_found}")
    x += 1
    time.sleep(15)
  • 3
    Maybe because at iteration 2589 there are no entries? Add an extra check: `if f.entries and f.entries[0].title ....`. – CristiFati Jun 10 '22 at 11:41
  • It always checks the last entry with the index [0]. new news are added on top. There is always an entry at this index... – Thore Wagner Jun 10 '22 at 11:43
  • 1
    Hmm, according to the error, in this situation there is no entry. – CristiFati Jun 10 '22 at 11:47
  • The [documentation](https://feedparser.readthedocs.io/en/latest/reference-entry.html) clearly states: "_This element always exists, although it may be an empty list._" So you should not assume that there is always an item in the list. – wovano Jun 14 '22 at 10:01

1 Answers1

-1

Maybe for this feed, f.entries was null ? It may be an idea to check if f.entries[0] is not null before doing some logic on it.