2017-10-28 111 views
0

Stackoverflow RSS源中的每個作業項都具有某些標籤,其中包含關鍵字「category」。使用Python中的Feedparser解析Stackoverflow相同名稱元素的RSS作業源

展望基本上是這樣的:

<category>scala</category> 
<category>hadoop</category> 
<category>apache-spark</category> 
<category>hive</category> 
<category>json</category> 

我想用Feedparser,把所有的標籤放入一個列表。相反,我總是得到第一個元素。 Feedparser文檔提到entries[i].content,但我不確定這是否正確,或者在這種情況下如何使用它。

這裏是我的代碼:

import feedparser 

rss_url = "https://stackoverflow.com/jobs/feed" 
feed = feedparser.parse(rss_url) 
items = feed["items"] 

for item in items: 
    title = item["title"] 
    try: 
     tags = [] 
     tags.append(item["category"]) 
     print(title + " " + str(tags)) 
    except: 
     print("Failed") 

回答

2

category上feedparser項目基本上是在tags列表中的第一個元素,這基本上是更feedparser項目列表的別名,每一個包含term屬性標籤名稱。

您只需直接訪問條款:

categories = [t.term for t in item.get('tags', [])] 

爲您的代碼是:

for item in items: 
    title = item["title"] 
    categories = [t.term for t in item.get('tags', [])] 
    print(title, ', '.join(categories)) 

entries[i].tags documentation

+0

解決了!謝謝。 – Felix

相關問題