Python:如何使用这个JSON API捕获404错误?

我正在编写一些使用此接口的脚本:http://www.robin-stocks.com/en/latest/functions.html

在我的一个模块中,我尝试运行以下函数来获取一组JSON响应并将它们存储在一个文件中。

import robin_stocks as r 

def getAllOptions(symbol):
        
        jsonAllOptions = r.options.find_tradable_options(symbol, expirationDate=None, strikePrice=None, optionType=None, info=None)
        
        with open('allOptions.json', 'w') as json_allop:
            json.dump(jsonAllOptions, json_allop)
        unfilteredOptions = pd.read_json (r'allOptions.json')
        allOptions = unfilteredOptions.loc[unfilteredOptions['rhs_tradability'] == 'untradable']
        return allOptions

def storeEachOption(allOptions):
    
    pathPrefix = "data/stockData/availableOptions/"
    pathSuffix = ".json"
    
    for sID in allOptions['id'].tolist():
        jsonCurrentOption = r.options.get_option_market_data_by_id(sID,info=None)
        
        fullPath = pathPrefix + sID + pathSuffix
        print('Writing....' + fullPath)
        with open(fullPath, 'w') as json_oplist:
            json.dump(jsonCurrentOption, json_oplist)
                
        pdCurrentOption = pd.DataFrame(jsonCurrentOption, index=[0])

然后我用以下命令呼叫他们:

storeEachOption(getAllOptions('TSLA'))

我的其他模块通常会提取这些JSON文件,并将它们转换为Pandas DataFrames来操作它们。问题是,我从罗宾汉的API中得到了一堆404。我认为这与我的代码有关,但是很明显,每当您尝试通过API获取可用选项列表时,它都会返回一些无效的选项it,因此返回了404。

我正在寻找一种方法来捕获这些404并将它们发送到/dev/null。最好是在将其保存到JSON文件之前发生,但如果不是这样,也没问题。当我在storeEachOption()中调用这行代码时,我开始在我的控制台中得到404个错误(由robin_stocks输出):

jsonCurrentOption = r.options.get_option_market_data_by_id(sID,info=None)

控制台输出:

404 Client Error: Not Found for url: https://api.robinhood.com/marketdata/options/b81b5b6c-3f2b-45f2-a5cf-fdf44e1fed85/

下面我列出了一个404和一个普通响应的样例JSON响应(以及每个文件中存储的内容)。

404响应:

{"adjusted_mark_price": "", "ask_price": "", "ask_size": "", "bid_price": "", "bid_size": "", "break_even_price": "", "high_price": "", "instrument": "", "last_trade_price": "", "last_trade_size": "", "low_price": "", "mark_price": "", "open_interest": "", "previous_close_date": "", "previous_close_price": "", "volume": "", "chance_of_profit_long": "", "chance_of_profit_short": "", "delta": "", "gamma": "", "implied_volatility": "", "rho": "", "theta": "", "vega": "", "high_fill_rate_buy_price": "", "high_fill_rate_sell_price": "", "low_fill_rate_buy_price": "", "low_fill_rate_sell_price": ""}

正常响应:

{"adjusted_mark_price": "0.010000", "ask_price": "0.620000", "ask_size": 110, "bid_price": "0.000000", "bid_size": 0, "break_even_price": "4.990000", "high_price": null, "instrument": "https://api.robinhood.com/options/instruments/00b70671-97d2-44cf-ad30-278f1c84ed1e/", "last_trade_price": null, "last_trade_size": null, "low_price": null, "mark_price": "0.310000", "open_interest": 0, "previous_close_date": "2020-07-01", "previous_close_price": "0.010000", "volume": 0, "chance_of_profit_long": "0.020993", "chance_of_profit_short": "0.979007", "delta": "-0.010636", "gamma": "0.011169", "implied_volatility": "0.806188", "rho": "-0.000126", "theta": "-0.000823", "vega": "0.000878", "high_fill_rate_buy_price": "0.450000", "high_fill_rate_sell_price": "0.020000", "low_fill_rate_buy_price": "0.210000", "low_fill_rate_sell_price": "0.270000"}

任何帮助都将不胜感激。

转载请注明出处:http://www.qxdxgs.com/article/20230526/2444204.html