1

I am trying to use feedparser with python to fetch the most recent posts from a sub_reddit.

I have the code below but it is not returning anything when I run it.

import feedparser

feed = feedparser.parse("http://www.reddit.com/r/funny/new/.rss")
#feed = feedparser.parse("http://feeds.bbci.co.uk/news/england/london/rss.xml")

feed_entries = feed.entries

for entry in feed.entries:
    article_title = entry.title
    article_link = entry.link
    article_published_at = entry.published # Unicode string
    article_published_at_parsed = entry.published_parsed # Time object
    print (article_title)
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
Charlie Morton
  • 737
  • 1
  • 7
  • 17

2 Answers2

4

I think this is related to a previous entry regarding an SSL issue with feedparser parsing an HTTPS RSS feed - https://stackoverflow.com/a/28296087/1627968

Adding the following code remedies the SSL issue:

import ssl
if hasattr(ssl, '_create_unverified_context'):
    ssl._create_default_https_context = ssl._create_unverified_context

For example, in your code:

import feedparser
import ssl
if hasattr(ssl, '_create_unverified_context'):
    ssl._create_default_https_context = ssl._create_unverified_context
feed = feedparser.parse("http://www.reddit.com/r/funny/new/.rss")
#feed = feedparser.parse("http://feeds.bbci.co.uk/news/england/london/rss.xml")

feed_entries = feed.entries
for entry in feed.entries:
    article_title = entry["title"]
    article_link = entry["link"]
    print(f"{article_title}: {article_link}")

You might want to check the keys you use for each entry - published didn't appear to be one of them, hence me removing it in my example.

Benjamin Rowell
  • 1,371
  • 8
  • 16
0

Consider changing:

article_published_at = entry.published # Unicode string
article_published_at_parsed = entry.published_parsed # Time object

to:

article_published_at = entry.updated
article_updated_parsed = entry.updated_parsed 

feed = feedparser.parse("https://www.reddit.com/r/funny/new/.rss")
for entry in feed.entries:
    article_title = entry.title
    article_link = entry.link
    article_published_at = entry.updated
    article_updated_parsed = entry.updated_parsed
    print (article_published_at)
    print (article_updated_parsed)

2018-12-10T22:26:20+00:00
time.struct_time(tm_year=2018, tm_mon=12, tm_mday=10, tm_hour=22, tm_min=26, tm_sec=20, tm_wday=0, tm_yday=344, tm_isdst=0)
...

Also make sure you use https protocol, just in case feedparser doesn't follow http->https redirects properly.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268