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

2024-06-07 21:51:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用yahoo finance API获取期权合约的出价和要价,但是JSON输出很难导航。你知道吗

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

有多个合同符号,例如:

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

它返回每个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}

我只是试图恢复每个contractSymbol的'bid和'ask',但是,它似乎嵌套在json中的很远,我遇到了很多麻烦。你知道吗


Tags: importapijsonurlrequestsyahooask期权
2条回答
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"])

我认为@Dodge的答案让python的新用户感到困惑

您可以分解解析JSON得到的dict,并将感兴趣的信息存储在一个列表中。以下是一个示例:

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

这将打印以下内容供您参考:

[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

相关问题 更多 >

    热门问题