【问题标题】:RSS Feed aggregator using Google App Engine - Python使用 Google App Engine 的 RSS Feed 聚合器 - Python
【发布时间】:2011-01-14 12:25:25
【问题描述】:

我正在尝试构建一个处理 RSS 提要并将提要中的所有数据存储到 Google 数据存储区的 GAE 应用程序。我使用 Minidom 从 RSS 提要中提取内容。我也尝试过使用 Feedparser 和 BeautifulSoup,但它们对我不起作用。

我的应用程序当前解析提要并在我的本地计算机上大约 25 秒内将其保存在 Google 数据存储区中。我上传了应用程序,当我尝试使用它时,我得到了“DeadLine Exceeded Error”。

我想知道是否有任何可能的方法来加快这个过程?随着时间的推移,我使用的提要最终会增长到超过 100 项。

【问题讨论】:

    标签: python rss feed


    【解决方案1】:

    我有一个使用 Feedparser - http://deliciourss.appspot.com/ 的 GAE RSS 阅读器演示/原型。这是一些代码 -

    获取您的供稿。

    data = urlfetch.fetch(feedUrl)
    

    使用 Feedparser 解析

    parsedData = feedparser.parse(data.content)
    

    更改提要的一些功能

        # set main section to description if empty
        for ix in range(len(parsedData.entries)):
            bItem = 0
            if hasattr(parsedData.entries[ix],'content'):
                for item in parsedData.entries[ix].content:
                    if item.value:
                        bItem = 1
                        break
                if bItem == 0:
                    parsedData.entries[ix].content[0].value = parsedData.entries[ix].summary
            else:
                parsedData.entries[ix].content = [{'value':parsedData.entries[ix].summary}]
    

    如果您使用的是 Django/webapp,请使用模板

    <?xml version="1.0" encoding="utf-8"?>
    <channel>
    <title>{{parsedData.channel.title}}</title>
    <url>{{feedUrl}}</url>
    <id>{{parsedData.channel.id}}</id>
    <updated>{{parsedData.channel.updated}}</updated>
    {% for entry in parsedData.entries %}
    <item>
            <id>{{entry.id}}</id>
            <title>{{entry.title}}</title>
            <link>
            {% for link in entry.links %}
                    {% ifequal link.rel "alternate" %}
                            {{link.href|escape}}
                    {% endifequal %}
            {% endfor %}
            </link>
            <author>{{entry.author_detail.name}}</author>
            <pubDate>{{entry.published}}</pubDate>
            <description>{{entry.summary|escape}}</description>
            {% for item in entry.content %}
                {% if item.value %} 
                   <content>{{item.value|escape}}</content>
                {% endif %}
            {% endfor %}
    </item>{% endfor %}
    </channel>
    

    【讨论】:

      【解决方案2】:

      我找到了解决此问题的方法,但我不确定这是否是最佳解决方案。

      我使用 cElementTree 来解析 RSS 提要,而不是 Minidom。我在一个单独的任务中处理每个“项目”标签及其子标签,并将这些任务添加到任务队列中。

      这帮助我避免了 DeadlineExceededError。我收到“此资源使用大量 CPU 资源”警告。

      关于如何避免警告的任何想法?

      A_iyer

      【讨论】:

        【解决方案3】:

        它不应该花这么长时间。以下是您可以如何使用Universal Feed Parser

        # easy_install feedparser
        

        还有一个使用它的例子:

        import feedparser
        
        feed = 'http://stackoverflow.com/feeds/tag?tagnames=python&sort=newest'
        d = feedparser.parse(feed)
        for entry in d['entries']:
            print entry.title
        

        文档向您展示了如何从提要中提取其他内容。如果您有具体问题,请发布详细信息。

        【讨论】:

        • 感谢您的回复 DisplacedAussie。 Feedparser 的一个问题是我无法获取标签的属性。你能告诉我怎么做吗?
        猜你喜欢
        • 1970-01-01
        • 2011-07-18
        • 1970-01-01
        • 2012-11-14
        • 2011-07-09
        • 1970-01-01
        • 2014-03-29
        • 2012-02-26
        • 1970-01-01
        相关资源
        最近更新 更多