Search code examples
pythonshadowing

How to get this json data using Python and put into pandas dataframe?


I am trying to get a JSON data every 1-minute using python and put it into pandas dataframe. The issue is only I am getting the data only the first time and then I am getting errors(Retrying). The code is here:

import requests
import json
import pandas as pd
import time

exp_date = '13-04-2022'


headers_gen = {"accept-encoding": "gzip, deflate, br",
               "accept-language": "en-US,en;q=0.9",
               "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"}


def pcr(exp_date):
    url_pcr = "https://www.indiainfoline.com/api/papi-call-api.php?url=%2FDerivative%2FDerivative.svc%2FGet-Put-Call-Ratio-version3%2Fidx%2Foi%2F" + exp_date + "%2Fall%2Fall%2Fsymbol%2FDesc%3Fresponsetype%3Djson"

    req_pcr = requests.get(url_pcr, headers=headers_gen)
    text_data_pcr= req_pcr.text
    json_dict_pcr= json.loads(text_data_pcr)

    df_pcr = pd.DataFrame.from_dict(json_dict_pcr['response']['data']['getputcallratioversion3list']['getputcallratioversion3'])
    return df_pcr


while True:

    try:

        pcr = pcr(exp_date)
        print(pcr)
        print("All OK")
        time.sleep(60)

    except:
        print("Retrying")
        time.sleep(5)

The URL from where I want to download data is can be changed by putting separate expiry dates so I created a variable called exp_date.

Can anyone help me to point out where I am making the error?


Solution

  • To help figure out the answer inside a try/except thing, use the code below to output exception details:

    except Exception as e:
        print("Retrying : {}".format(e))
        time.sleep(5)
    

    then i get Retrying : 'DataFrame' object is not callable

    the following line is the problem: pcr = pcr(exp_date)

    You've given a function and a variable the same name.

    all ive done to fix it is change the function pcr to PCR

    def PCR(exp_date):

    and the line:

    pcr = PCR(exp_date)