我正在尝试将feedparser与python
一起使用,以从sub_reddit获取最新的帖子。
我有下面的代码,但运行时它不返回任何内容。
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)
答案 0 :(得分:3)
我认为这与先前有关feedparser解析HTTPS RSS feed的SSL问题的条目有关-https://stackoverflow.com/a/28296087/1627968
添加以下代码可解决SSL问题:
import ssl
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
例如,在您的代码中:
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}")
您可能要检查用于每个条目的键-发布的键似乎不是其中之一,因此我在示例中将其删除。
答案 1 :(得分:0)
考虑更改:
article_published_at = entry.published # Unicode string
article_published_at_parsed = entry.published_parsed # Time object
收件人:
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)
...
还要确保您使用https
协议,以防万一feedparser
不遵循http
-> https
重定向。< / p>