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)