我收到此错误: 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
提交
取消