Python - Fuente RSS

RSS (Rich Site Summary) es un formato para entregar contenido web que cambia regularmente. Muchos sitios relacionados con noticias, weblogs y otros editores en línea distribuyen su contenido como una fuente RSS para quien lo desee. En Python, tomamos la ayuda del siguiente paquete para leer y procesar estos feeds.

pip install feedparser

Estructura de alimentación

En el siguiente ejemplo, obtenemos la estructura del feed para poder analizar más a fondo qué partes del feed queremos procesar.

import feedparser
NewsFeed = feedparser.parse("https://timesofindia.indiatimes.com/rssfeedstopstories.cms")
entry = NewsFeed.entries[1]
print entry.keys()

Cuando ejecutamos el programa anterior, obtenemos el siguiente resultado:

['summary_detail', 'published_parsed', 'links', 'title', 'summary', 'guidislink', 'title_detail', 'link', 'published', 'id']

Título y publicaciones del feed

En el siguiente ejemplo, leemos el título y el encabezado del feed rss.

import feedparser
NewsFeed = feedparser.parse("https://timesofindia.indiatimes.com/rssfeedstopstories.cms")
print 'Number of RSS posts :', len(NewsFeed.entries)
entry = NewsFeed.entries[1]
print 'Post Title :',entry.title

Cuando ejecutamos el programa anterior, obtenemos el siguiente resultado:

Number of RSS posts : 5
Post Title : Cong-JD(S) in SC over choice of pro tem speaker

Detalles del feed

Basándonos en la estructura de entrada anterior, podemos derivar los detalles necesarios del feed usando el programa Python como se muestra a continuación. Como la entrada es un diccionario, utilizamos sus claves para producir los valores necesarios.

import feedparser
NewsFeed = feedparser.parse("https://timesofindia.indiatimes.com/rssfeedstopstories.cms")
entry = NewsFeed.entries[1]
print entry.published
print "******"
print entry.summary
print "------News Link--------"
print entry.link

Cuando ejecutamos el programa anterior, obtenemos el siguiente resultado:

Fri, 18 May 2018 20:13:13 GMT
******
Controversy erupted on Friday over the appointment of BJP MLA K G Bopaiah as pro tem speaker for the assembly, with Congress and JD(S) claiming the move went against convention that the post should go to the most senior member of the House. The combine approached the SC to challenge the appointment. Hearing is scheduled for 10:30 am today.
------News Link--------
https://timesofindia.indiatimes.com/india/congress-jds-in-sc-over-bjp-mla-made-pro-tem-speaker-hearing-at-1030-am/articleshow/64228740.cms