为了账号安全,请及时绑定邮箱和手机立即绑定

Python feedparser 模块无法将数据追加到字典中

Python feedparser 模块无法将数据追加到字典中

慕哥6287543 2023-03-30 17:13:09
我收到此错误: 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


查看完整回答
反对 回复 2023-03-30
  • 1 回答
  • 0 关注
  • 96 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信