我收到此錯誤: AttributeError: 'NoneType' object has no attribute 'append' 我試圖將所有條目值存儲在一個字典中,因為我創建了一個函數:rss_url = 'https://www.espn.com/espn/rss/' + league + '/news' parser = feedparser.parse(rss_url) newsInfo = { 'title': None, 'link': None, 'description': None, 'image': None } for entry in parser.entries: newsInfo['title'].append(entry.title) newsInfo['link'].append(entry.links[0].href) newsInfo['description'].append(entry.description) newsInfo['image'].append(entry.content[0].value) return newsInfo但是,在我使用的行中,.append出現了 NoneType 錯誤。獎勵問題:如果我將來自 feedparser 的值呈現到 HTML 上,它會正確顯示新聞嗎,還是會有另一個步驟?
1 回答

慕碼人2483693
TA貢獻1860條經驗 獲得超9個贊
您要么想將它們初始化為列表:
newsInfo = {
'title': [],
'link': [],
'description': [],
'image': []
}
或者您想在 for 循環中分配值(取決于您的用例):
for entry in parser.entries:
newsInfo['title'] = entry.title
newsInfo['link'] = entry.links[0].href
newsInfo['description'] = entry.description
newsInfo['image'] = entry.content[0].value
添加回答
舉報
0/150
提交
取消