【发布时间】:2017-02-17 03:19:31
【问题描述】:
尽管我浏览了文档,但在理解 jsonify 的工作原理时遇到了一些麻烦。正如您在下面看到的,我正在调用返回字典的lookup() 函数,然后我正在尝试对其进行jsonify。
@app.route("/articles")
def articles():
a = lookup(33496)
return jsonify([link=a["link"], title = a["title"]]) #invalid syntax error
我的helpers.py:
import feedparser
import urllib.parse
def lookup(geo):
"""Looks up articles for geo.""" #this function already parses the 'link' and 'title' form rss feed
# check cache for geo
if geo in lookup.cache:
return lookup.cache[geo]
# get feed from Google
feed = feedparser.parse("http://news.google.com/news?geo={}&output=rss".format(urllib.parse.quote(geo, safe="")))
# if no items in feed, get feed from Onion
if not feed["items"]:
feed = feedparser.parse("http://www.theonion.com/feeds/rss")
# cache results
lookup.cache[geo] = [{"link": item["link"], "title": item["title"]} for item in feed["items"]]
# return results
return lookup.cache[geo]
# initialize cache
lookup.cache = {}
我得到的错误是无效的语法。知道我做错了什么吗?谢谢
【问题讨论】: