2011-03-29 54 views
0

我是Python新手。我有使用Feedparser的代碼:Python和FeedParser問題

import feedparser 

d = feedparser.parse('http://pplware.sapo.pt/feed/') 

i = range(10) 

for i in range(10): 
    updates = [{"url": d.entries[i].link, "msg": d.entries[i].summary + ", "}] 

我的問題。

如何添加到變量「更新」的10個條目?

最好的問候,

回答

1
import feedparser 

d = feedparser.parse('http://pplware.sapo.pt/feed/')  
updates = [] 
for i in range(10): 
    updates.append({"url": d.entries[i].link, "msg": d.entries[i].summary + ", "}) 

在你的循環,你追加dictionnary結果的更新列表。 'updates'包含for循環之後的10個元素。每個條目一個。

編輯:

然而,RestRisiko提供的代碼確實更漂亮;-)

+0

謝謝!有效。 – 2011-03-29 17:23:40

0
d = feedparser.parse('http://pplware.sapo.pt/feed/') 
for item in d.entries[:10]: 
    print item 

提取從「項目」的信息並將其添加到列表或字典是 基本用法Python列表和字典(請閱讀本例中的教程 - 我們不能在這裏教授列表和字典的基本用法)。

+0

感謝您的回覆。它需要將值存儲在一個變量中。 – 2011-03-29 17:24:16

+0

然後做吧! :) – 2011-03-29 18:01:00