如何在 Python 3.7 中构建一个简单的 RSS 阅读器?

Kad*_*dio 8 python rss feedparser python-3.7

我在 Python 上构建了一个简单的 RSS 阅读器,但它不起作用。另外,我想获取每个帖子的精选图片源链接,但我没有找到方法。

它向我显示了错误:回溯(最近一次通话):文件“RSS_reader.py”,第 7 行,在 feed_title = feed['feed']['title'] 中

如果有其他一些 RSS 提要可以正常工作。所以我不明白为什么有些 RSS 提要有效,而另一些则无效

所以我想了解为什么代码不起作用,以及如何获取我附上代码的帖子的特色图片源链接,是在 Python 3.7 上编写的

import feedparser
import webbrowser

feed = feedparser.parse("https://finance.yahoo.com/rss/")

feed_title = feed['feed']['title']
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
    article_author = entry.author
    content = entry.summary
    article_tags = entry.tags


    print ("{}[{}]".format(article_title, article_link))
    print ("Published at {}".format(article_published_at))
    print ("Published by {}".format(article_author))
    print("Content {}".format(content))
    print("catagory{}".format(article_tags))
Run Code Online (Sandbox Code Playgroud)

Bru*_*len 8

一些东西。

1)第一个feed['feed']['title']不存在。
2) 至少对于这个站点entry.author, entry.tags不存在
3) 似乎 feedparser 与 python3.7 不兼容(它给了我KeyError, "object doesn't have key 'category'

因此,作为起点,尝试在 python 3.6 中运行以下代码并从那里开始。

import feedparser
import webbrowser

feed = feedparser.parse("https://finance.yahoo.com/rss/")

# feed_title = feed['feed']['title']  # NOT VALID
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
    # article_author = entry.author  DOES NOT EXIST
    content = entry.summary
    # article_tags = entry.tags  DOES NOT EXIST


    print ("{}[{}]".format(article_title, article_link))
    print ("Published at {}".format(article_published_at))
    # print ("Published by {}".format(article_author)) 
    print("Content {}".format(content))
    # print("catagory{}".format(article_tags))
Run Code Online (Sandbox Code Playgroud)

祝你好运。


小智 5

您还可以使用 xml 解析器库,如 beatifulsoup ( https://www.crummy.com/software/BeautifulSoup/bs4/doc/ ) 并创建自定义解析器。可以在此处找到示例客户解析器代码 ( https://github.com/vintageplayer/RSS-Parser )。可以在这里阅读(https://towardsdatascience.com/rss-feed-parser-in-python-553b1857055c

虽然库可能很有用,beautifulsoup 是一个非常方便的库,可以尝试。