从Python输出获取单行

时间:2012-02-09 05:24:17

标签: python

所以,在我的last question中,我请求帮助解析RSS提要中的XML链接。利用我从这里获得的帮助中获得的想法以及额外的研究,我能够写下这个:

def GetRSS(RSSurl):
    url_info = urllib.urlopen(RSSurl)
    if (url_info):
        xmldoc = minidom.parse(url_info)
    if (xmldoc):
        channel = xmldoc.getElementsByTagName('channel')
        for node in channel:
            item = xmldoc.getElementsByTagName('item')
            for node in item:
                alist = xmldoc.getElementsByTagName('link')
                for a in alist: 
                    linktext = a.firstChild.data
                    print linktext

正如我在另一个问题中提到的,我写这篇文章是为了从RSS feed on Redlettermedia.com获取链接。代码工作正常,我收到的输出是:

http://redlettermedia.com
http://redlettermedia.com/half-in-the-bag-b-fest-2012/
http://redlettermedia.com/an-update-from-red-letter-media/
http://redlettermedia.com/half-in-the-bag-red-tails/
http://redlettermedia.com/half-in-the-bag-the-devil-inside-and-flyin-ryan/
http://redlettermedia.com/newly-found-episode-iii-review-behind-the-scenes-footage/
http://redlettermedia.com/half-in-the-bag-the-girl-with-the-dragon-tattoo-and-2011-re-cap/
http://redlettermedia.com/mr-plinetts-indiana-jones-and-the-kingdom-of-the-crystal-skull-review/
http://redlettermedia.com/new-mr-plinkett-review-trailer/
http://redlettermedia.com/plinkett-fest/
http://redlettermedia.com/update/
http://redlettermedia.com
http://redlettermedia.com/half-in-the-bag-b-fest-2012/
http://redlettermedia.com/an-update-from-red-letter-media/
http://redlettermedia.com/half-in-the-bag-red-tails/
http://redlettermedia.com/half-in-the-bag-the-devil-inside-and-flyin-ryan/
http://redlettermedia.com/newly-found-episode-iii-review-behind-the-scenes-footage/

等等。我现在要做的是打印最新的更新链接作为函数的结果(输出中的第二行,在这种情况下为“http://redlettermedia.com/half-in-the-bag-b-fest-2012/”)。我怎么才打印那条线?

1 个答案:

答案 0 :(得分:1)

如果它始终是列表中的第二项,您可以尝试

url = xmldoc.getElementsByTagName('link')[1].firstChild.data
print url