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

我收到此错误: 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 上,它会正确显示新闻吗,还是会有另一个步骤?


慕哥6287543
浏览 97回答 1
1回答

慕码人2483693

您要么想将它们初始化为列表: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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python