[英]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).我正在尝试使用 python 每 1 分钟获取一次 JSON 数据并将其放入 pandas dataframe。问题只是我第一次获取数据然后我收到错误(重试)。 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.我想从中下载数据的 URL 可以通过放置单独的到期日期来更改,所以我创建了一个名为 exp_date 的变量。
Can anyone help me to point out where I am making the error?谁能帮我指出我在哪里犯了错误?
To help figure out the answer inside a try/except thing, use the code below to output exception details:为了帮助找出 try/except 事物中的答案,请使用以下代码获取 output 异常详细信息:
except Exception as e:
print("Retrying : {}".format(e))
time.sleep(5)
then i get Retrying: 'DataFrame' object is not callable
然后我得到
Retrying: 'DataFrame' object is not callable
the following line is the problem: pcr = pcr(exp_date)
下一行是问题所在:
pcr = pcr(exp_date)
You've given a function and a variable the same name.你给了一个 function 和一个同名的变量。
all ive done to fix it is change the function pcr to PCR我所做的就是将 function pcr 更改为 PCR
def PCR(exp_date):
and the line:和线:
pcr = PCR(exp_date)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.