Jeremy Hunt
Jeremy Hunt

Reputation: 21

Check if there is anything new in RSS Feed

How to check if there is anything new in feed? my code is:

import time
import requests
from bs4 import BeautifulSoup

url = input("Enter your feed url: ")
resp = requests.get(url)

soup = BeautifulSoup(resp.content, features="xml")

items = soup.findAll('item')
news_item = {}
        news_item['title'] = item.title.text
        news_item['description'] = item.description.text
        news_item['link'] = item.link.text

i want the check if there is any new post in rss feed or not

Upvotes: 0

Views: 399

Answers (1)

Noah
Noah

Reputation: 193

I had to solve a similar problem once. Since my RSS feed wasn't that big, I just created a .txt file locally, loaded it at the beginning, and wrote to it at the end. But depending on how many entries you have, this can result in performance issues.

Another thing you could do is to save only the date of the latest post in a file or in a database, and compare the dates of the entries in the feed to the date of the "oldest" file you already saw.

Could be something like:

last_date = ""
with open("date.txt", "r") as d:
    last_date = d.read()

and down below in your loop:

if item.date.text > last_date:
    continue

I hope I could help you. Contact me if you have any questions.

Upvotes: 2

Related Questions