简体   繁体   English

如何从Python中的json api获取特定数据

[英]How to get get specific data from json api in Python

I am trying to get the bid and ask price for an options contract using the yahoo finance API, however, the JSON output is difficult to navigate.我正在尝试使用雅虎金融 API 获取期权合约的出价和要价,但是,JSON 输出难以导航。

import requests
url = requests.get("https://query1.finance.yahoo.com/v7/finance/options/AAPL").json()
url

There a multiple contractSymbol such as:有多个 contractSymbol,例如:

'contractSymbol': 'AAPL190503C00150000'
'contractSymbol': 'AAPL190503C00155000'

and it returns this associated data for each contractSymbol.并为每个 contractSymbol 返回此关联数据。

{'contractSymbol': 'AAPL190503C00150000',
        'strike': 150.0,
        'currency': 'USD',
        'lastPrice': 54.31,
        'change': -1.579998,
        'percentChange': -2.826978,
        'volume': 105,
        'openInterest': 35,
        'bid': 52.25,
        'ask': 54.85,
        'contractSize': 'REGULAR',
        'expiration': 1556841600,
        'lastTradeDate': 1556306875,
        'impliedVolatility': 1.4033232958984376,
        'inTheMoney': True}

I am only trying to recover the 'bid and 'ask' for each contractSymbol, however, it seems to be nested very far into the json and I am having a lot of trouble.我只是想恢复每个 contractSymbol 的“出价和询问”,但是,它似乎嵌套在 json 中很远的地方,而且我遇到了很多麻烦。

You can take apart the dict that results from parsing the JSON and store the information of interest in a list.您可以拆开解析JSON所产生的 dict,并将感兴趣的信息存储在列表中。 The following is an example:下面是一个例子:

import requests

r = requests.get("https://query1.finance.yahoo.com/v7/finance/options/AAPL")
data = r.json()

# "option_chain" is a list holding everything.
option_chain = data['optionChain']

# "result" is a dictionary, the first item in "option_chain".
result = option_chain['result'][0]

# These are the components of the "result" dictionary.
underlyingSymbol = result['underlyingSymbol']
expirationDates = result['expirationDates']
strikes = result['strikes']
hasMiniOptions = result['hasMiniOptions']
quote = result['quote']
options = result['options'][0]

# This is the list of dictionaries that interest you.
calls = options['calls']

con_symbs = []
bids = []
asks = []

for call_dict in calls:
    contract_symbol = call_dict['contractSymbol']
    bid = call_dict['bid']
    ask = call_dict['ask']

    con_symbs.append(contract_symbol)
    bids.append(bid)
    asks.append(ask)

for (cs,b,a) in zip(con_symbs,bids,asks):
    print('[INFO] {0}: bid: {1} ask: {2}'.format(cs,b,a))

This will print, for your reference, the following:这将打印以下内容供您参考:

[INFO] AAPL190503C00150000: bid: 52.25 ask: 54.85
[INFO] AAPL190503C00155000: bid: 47.25 ask: 51.25
[INFO] AAPL190503C00160000: bid: 44.05 ask: 44.9
[INFO] AAPL190503C00165000: bid: 37.25 ask: 41.5
[INFO] AAPL190503C00167500: bid: 34.8 ask: 39.0
[INFO] AAPL190503C00170000: bid: 33.6 ask: 34.9
[INFO] AAPL190503C00172500: bid: 29.95 ask: 34.05
[INFO] AAPL190503C00175000: bid: 29.3 ask: 29.95
[INFO] AAPL190503C00177500: bid: 25.0 ask: 28.85
[INFO] AAPL190503C00180000: bid: 24.2 ask: 25.05
[INFO] AAPL190503C00182500: bid: 22.0 ask: 22.4
[INFO] AAPL190503C00185000: bid: 19.6 ask: 19.85
[INFO] AAPL190503C00187500: bid: 17.2 ask: 17.5
[INFO] AAPL190503C00190000: bid: 14.95 ask: 15.2
[INFO] AAPL190503C00192500: bid: 12.75 ask: 13.05
[INFO] AAPL190503C00195000: bid: 10.75 ask: 11.0
[INFO] AAPL190503C00197500: bid: 8.9 ask: 9.05
[INFO] AAPL190503C00200000: bid: 7.2 ask: 7.35
[INFO] AAPL190503C00202500: bid: 5.7 ask: 5.85
[INFO] AAPL190503C00205000: bid: 4.35 ask: 4.45
[INFO] AAPL190503C00207500: bid: 3.2 ask: 3.35
[INFO] AAPL190503C00210000: bid: 2.25 ask: 2.3
[INFO] AAPL190503C00212500: bid: 1.49 ask: 1.54
[INFO] AAPL190503C00215000: bid: 0.94 ask: 0.99
[INFO] AAPL190503C00217500: bid: 0.59 ask: 0.62
[INFO] AAPL190503C00220000: bid: 0.35 ask: 0.39
[INFO] AAPL190503C00222500: bid: 0.23 ask: 0.25
[INFO] AAPL190503C00225000: bid: 0.14 ask: 0.16
[INFO] AAPL190503C00230000: bid: 0.06 ask: 0.07
[INFO] AAPL190503C00235000: bid: 0.03 ask: 0.05
[INFO] AAPL190503C00240000: bid: 0.02 ask: 0.04
import requests
data = requests.get("https://query1.finance.yahoo.com/v7/finance/options/AAPL").json()
calls = data["optionChain"]["result"][0]["options"][0]["calls"]
for el in calls:
    print(el["contractSymbol"], el["bid"], el["ask"])

And I think @Dodge's answer is confusing for new python users我认为@Dodge 的回答让新的 Python 用户感到困惑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM