尝试解析用于Python编写的RSS阅读器的源数据

0 投票
2 回答
617 浏览
提问于 2025-04-17 08:09

我还是个Python初学者。作为一个练手项目,我想自己写一个RSS阅读器。我在这里找到了一篇很有帮助的教程:学习Python。我用了教程里提供的代码:

#! /usr/bin/env python    
import urllib2
from xml.dom import minidom, Node

""" Get the XML """
url_info = urllib2.urlopen('http://rss.slashdot.org/Slashdot/slashdot')

if (url_info):
    """ We have the RSS XML lets try to parse it up """
    xmldoc = minidom.parse(url_info)
    if (xmldoc):
        """We have the Doc, get the root node"""
        rootNode = xmldoc.documentElement
        """ Iterate the child nodes """
        for node in rootNode.childNodes:
            """ We only care about "item" entries"""
            if (node.nodeName == "item"):
                """ Now iterate through all of the <item>'s children """
                for item_node in node.childNodes:
                    if (item_node.nodeName == "title"):
                        """ Loop through the title Text nodes to get
                        the actual title"""
                        title = ""
                        for text_node in item_node.childNodes:
                            if (text_node.nodeType == node.TEXT_NODE):
                                title += text_node.nodeValue
                        """ Now print the title if we have one """
                        if (len(title)>0):
                            print title

                    if (item_node.nodeName == "description"):
                        """ Loop through the description Text nodes to get
                        the actual description"""
                        description = ""
                        for text_node in item_node.childNodes:
                            if (text_node.nodeType == node.TEXT_NODE):
                                description += text_node.nodeValue
                        """ Now print the title if we have one.
                        Add a blank with \n so that it looks better """
                        if (len(description)>0):
                            print description + "\n"
    else:
        print "Error getting XML document!"
else:
    print "Error! Getting URL"<code>

一切都按预期工作,起初我觉得自己都明白了。但是当我使用另一个RSS源(比如“http://www.spiegel.de/schlagzeilen/tops/index.rss”)时,我的应用程序在Eclipse IDE中出现了“终止”错误。我也说不清楚这个错误信息,因为我搞不清楚应用程序到底在哪里和为什么会终止。调试工具也没什么帮助,因为它忽略了我的断点。嗯,这又是另一个问题。

有没有人知道我哪里做错了?

2 个回答

0

如果什么都没有发生,可能你的代码没问题,只是没有找到正确的元素 :)

如果出现了异常,试着从命令行启动:

python <yourfilename.py>

或者使用一个try/catch来捕获这个异常,并打印出错误信息:

try:
    # your code
catch Exception, e:
    # print it
    print 'My exception is', e
4

其实“terminated”这个信息并不是错误,它只是告诉你Python已经正常退出,没有发生错误。

你并没有做错什么,只是这个RSS阅读器不太灵活,因为它只支持一种RSS格式。

如果你比较一下Slashdot和Spiegel Online的XML文档,你会发现它们的结构有些不同:

Slashdot:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" ...>
  <channel rdf:about="http://slashdot.org/">
    <title>Slashdot</title>
    <!-- more stuff (but no <item>-tags) -->
  </channel>
  <item rdf:about="blabla">
    <title>The Condescending UI</title>
    <!-- item data -->
  </item>
  <!-- more <item>-tags -->
</rdf:RDF>

Spiegel Online:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
  <channel>
    <title>SPIEGEL ONLINE - Schlagzeilen</title>
    <link>http://www.spiegel.de</link>
    <item>
      <title>Streit über EU-Veto: Vize Clegg meutert gegen britischen Premier Cameron</title>
    </item>
    <!-- more <item>-tags -->
  <channel>
</rss>

在Spiegel Online的RSS源中,所有的<item>元素都在<channel>标签里,而在Slashdot的RSS源中,它们是在标签(<rdf:RDF>)里。你的Python代码只期待在标签里找到这些项目。

如果你想让你的RSS阅读器同时支持这两个源,你可以修改以下这一行:

for node in rootNode.childNodes:

改成这样:

for node in rootNode.getElementsByTagName('item'):

这样一来,所有的<item>标签都会被列出来,无论它们在XML文档中的位置如何。

撰写回答