Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python API JSON响应为空_Python_Json_Api_Flask - Fatal编程技术网

Python API JSON响应为空

Python API JSON响应为空,python,json,api,flask,Python,Json,Api,Flask,我的目标是使用Financialmodelingprep.com提供的API,使用flask运行python程序。我创建了一个路由并定义了一个函数,如文档中所述。我所做的唯一编辑是将stock符号作为参数传递到函数中,这样当我使用stock符号点击端点时,它将在浏览器的JSON对象中显示符号的数据 当我运行程序并点击端点时,我在anaconda终端中得到一个200响应,但浏览器只显示一个空的JSON响应,如:{} 这是我的密码: from flask import Flask try:

我的目标是使用Financialmodelingprep.com提供的API,使用flask运行python程序。我创建了一个路由并定义了一个函数,如文档中所述。我所做的唯一编辑是将stock符号作为参数传递到函数中,这样当我使用stock符号点击端点时,它将在浏览器的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)

要使用API获取google stock GOOG上的股票信息,通常需要使用以下内容:

答案是:

{
  "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)

我能够解决这个问题。变量“url”的位置需要更改。我需要将变量url移动到RunAction中的第一个变量:

从烧瓶进口烧瓶 尝试: 适用于Python 3.0及更高版本 从urllib.request导入urlopen 除恐怖外: 回到Python 2的urllib2 从urllib2导入urlopen

导入json

安装烧瓶类。name引用当前正在使用的模块的名称,该模块是hello-world.py app=烧瓶名称

@app.route'/stock/profile/' def get_jsonparsed_数据符号: url={}?apikey=5df07fe2e77eb907f2496af6f4a48260.formatsymbol


您是否检查过URL提供给您的内容(例如,如果您将其直接放在web浏览器地址栏上)?是的,我喜欢它的工作方式,但我使用API URL的目的是在我自己的应用程序上使用它。我正在构建一个微服务体系结构,这个API将帮助我实现一个微服务。你能编辑这个问题并证明你已经检查了API URL,并以读者可以验证它如何工作或应该如何工作的方式给你一个期望的响应吗?
response = urlopen(url)
data = response.read().decode("utf-8")
return json.loads(data)