【问题标题】:How to build a simple RSS reader in Python 3.7?如何在 Python 3.7 中构建一个简单的 RSS 阅读器?
【发布时间】:2019-09-20 00:23:48
【问题描述】:

我在 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))

【问题讨论】:

    标签: python rss python-3.7 feedparser


    【解决方案1】:

    一些事情。

    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))
    

    祝你好运。

    【讨论】:

    • 谁能确认现在是否支持 feedparser?它说它目前需要 Python >= 3.6。
    【解决方案2】:

    您还可以使用 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 是一个非常方便试用的库。

    【讨论】:

      【解决方案3】:

      我已经将 BeautifulSoup 用于一个初学者 RSS 提要阅读器项目(您需要安装 lxml 才能工作,因为我们正在处理 xml):

      from bs4 import BeautifulSoup
      import requests
      url = requests.get('https://realpython.com/atom.xml')
      
      soup = BeautifulSoup(url.content, 'xml')
      entries = soup.find_all('entry')
      
      for i in entries:
        title = i.title.text
        link = i.link['href']
        summary = i.summary.text
        print(f'Title: {title}\n\nSummary: {summary}\n\nLink: {link}\n\n------------------------\n') 
      

      您可以在此处找到 Youtube 视频: https://www.youtube.com/watch?v=8HbqO-TfjlI

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-07
        • 1970-01-01
        • 1970-01-01
        • 2012-09-20
        • 2016-08-31
        • 1970-01-01
        • 1970-01-01
        • 2015-02-24
        相关资源
        最近更新 更多