python-3.x - 类型错误:新闻对象不可迭代
问题描述
我正在尝试从新闻 api 获取新闻,我已将函数设置为返回新闻结果数组。在我想要显示新闻的显示模板中出现错误。
def getNews():
'''
function that fetches all news
'''
with urllib.request.urlopen(news_url+'281dbdc2e10e4a6ab51a9a27a614c146') as me:
news_data = me.read()
news_response = json.loads(news_data)
all_news = None
if news_response['articles']:
all_news_list = news_response['articles']
all_news_data = process_news(all_news_list)
return all_news
def process_news(news_list):
all_news = []
for news in news_list:
author = news.get('author')
title = news.get('title')
description = news.get('description')
url = news.get('url')
urlToImage = news.get('urlToImage')
publishedAt = news.get('publishedAt')
if author:
news_object = Article(author,title,description,url,urlToImage,publishedAt)
all_news.append(news_object)
return all_news
@main.route('/')
def index():
news = getNews()
title = "All News"
return render_template('index.html',news=news,title=title)
{% for myNews in news %}
{% endfor %}
解决方案
推荐阅读
- xcode - Bazel 无法运行 xcrun 但 xcode 和 xcrun 已在本地正确安装
- php - 是否可以使此 ASP.NET 到 ASP.NET SSO 功能与 ASP.NET 到 PHP 一起使用?
- javascript - 为什么一个反斜杠的 JSON.parse 返回两个反斜杠,而不是四个反斜杠
- vowpalwabbit - cb_explore 输入格式:在训练中使用提供概率值
- python - 如何使两台印刷机在 tkinter 中产生相同的输出
- javascript - 将 javascript 按钮添加到模态(javascript 新手)
- amp-html - AMP 中有没有办法在上传之前从输入表单中显示选定的图像?
- javascript - 如何修复此 jquery 安全警报:“您的应用包含一个或多个存在已知安全问题的库”
- python - 获取条形图中条形的标签
- ruby - 当我使用类 httparty 时,我遇到了 parsed_response 的问题