【问题标题】:API JSON response is blankAPI JSON 响应为空
【发布时间】: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 以读者可以验证其工作方式或应该如何工作的方式为您提供所需的响应?

标签: python json api flask


【解决方案1】:

要使用 API 获取有关谷歌股票 (GOOG) 的股票信息,通常会使用以下内容: https://financialmodelingprep.com/api/v3/company/profile/GOOG?apikey=yourApiKeyWouldGoHere

响应将是:

{
  "symbol" : "GOOG",
  "profile" : {
    "price" : 1410.42,
    "beta" : "1.022765",
    "volAvg" : "2447698",
    "mktCap" : "9.6415757E11",
    "lastDiv" : "0",
    "range" : "1013.536-1532.106",
    "changes" : 7.62,
    "changesPercentage" : "(+0.54%)",
    "companyName" : "Alphabet Inc.",
    "exchange" : "Nasdaq Global Select",
    "industry" : "Online Media",
    "website" : "https://www.abc.xyz",
    "description" : "Alphabet Inc is a provider of internet content products and portals. Its suite of brands includes Search, Android, YouTube, Apps, Maps & Ads.",
    "ceo" : "Larry Page",
    "sector" : "Technology",
    "image" : "https://financialmodelingprep.com/images-New-jpg/GOOG.jpg"
  }
}

我正在尝试将此 API 用于我正在创建的应用程序,以便用户可以在路由中提供符号,并且函数将符号作为参数来发出 GET 请求,如下所示:

@app.route('/stock/profile/<string:symbol>')
def get_jsonparsed_data(symbol):
    response = urlopen(url)
    data = response.read().decode("utf-8")
    return json.loads(data)
    print(data)
url = "https://financialmodelingprep.com/api/v3/company/profile/{}?apikey=yourApiKeyWouldGoHere".format(symbol)

【讨论】:

    【解决方案2】:

    我能够解决这个问题。需要更改的是变量“url”的位置。我需要将变量 url 移动为运行函数中的第一个变量:

    从烧瓶进口烧瓶 尝试: # 对于 Python 3.0 及更高版本 从 urllib.request 导入 urlopen 导入错误除外: # 回退到 Python 2 的 urllib2 从 urllib2 导入 urlopen

    导入 json

    实例化 Flask 类。 "name" 引用当前正在使用的模块的名称,即 hello-world.py

    app = Flask(名称)

    @app.route('/stock/profile/') def get_jsonparsed_data(符号): url = "https://financialmodelingprep.com/api/v3/company/profile/{}?apikey=5df07fe2e77eb907f2496af6f4a48260".format(symbol)

    response = urlopen(url)
    data = response.read().decode("utf-8")
    return json.loads(data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      相关资源
      最近更新 更多