因此,在我的last question中,我詢問了如何在RSS提要中解析XML中的鏈接。使用我從這裏與額外的研究相結合收到的援助的想法,我能寫了這個:從Python輸出獲取一行代碼
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/」)。我將如何只打印該行?
可以安裝非STDLIB模塊?你如何定義'最新的更新鏈接'? – Daenyth 2012-02-09 05:29:09