【发布时间】:2020-05-22 08:00:55
【问题描述】:
我的目标是使用我在 Financialmodelingprep.com 上使用的 API,使用烧瓶来运行我的 python 程序。我创建了一个路由并定义了一个函数,如文档中所述。我所做的唯一编辑是将股票代码作为参数传递给函数,这样当我用股票代码点击端点时,它将在浏览器的 JSON 对象中显示符号数据。
当我运行程序并点击端点时,我在 anaconda 终端中收到 200 响应,但浏览器只显示一个空的 JSON 响应,如下所示:{}
这是我的代码:
from flask import Flask
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
import json
#instatiating the Flask Class. "__name__" references the name of the current module being worked with which is hello-world.py
app = Flask(__name__)
# Instaniating a routeRoute
@app.route('/stock/profile/<string:symbol>')
def get_jsonparsed_data(symbol):
"""
Receive the content of ``url``, parse it as JSON and return the object.
Parameters
----------
url : str
Returns
-------
dict
"""
response = urlopen(url)
data = response.read().decode("utf-8")
return json.loads(data)
symbol=None
url = "https://financialmodelingprep.com/api/v3/company/profile/{}?apikey=myapikeywashere".format(symbol)
【问题讨论】:
-
你检查过 URL 给你的信息吗?如果你直接把它放在你的网络浏览器地址栏?
-
是的,我喜欢它的工作方式,但我使用 API URL 的目的是在我自己的应用程序中使用它。我正在构建一个微服务架构,这个 API 将帮助我实现一个微服务。
-
您能否编辑问题并证明您已检查 API URL 以读者可以验证其工作方式或应该如何工作的方式为您提供所需的响应?