使用python post请求处理错误502

使用python post请求处理错误502,post,python-requests,try-catch,Post,Python Requests,Try Catch,尝试构建一个简单的脚本来下载具有多个post请求的数据。 有时由于响应[504]而导致错误 我尝试了一些try/except来处理此错误,但不知何故,我没有捕捉到事件 我附加了没有异常处理的代码和错误的图片。 我知道错误来自JSON,因为由于错误的响应错误,没有数据可解码 有什么想法吗 import pandas as pd import requests import time import os from pandas.io.json import json_normalize df3 =

尝试构建一个简单的脚本来下载具有多个post请求的数据。 有时由于响应[504]而导致错误

我尝试了一些try/except来处理此错误,但不知何故,我没有捕捉到事件

我附加了没有异常处理的代码和错误的图片。 我知道错误来自JSON,因为由于错误的响应错误,没有数据可解码

有什么想法吗

import pandas as pd
import requests
import time
import os
from pandas.io.json import json_normalize

df3 = pd.DataFrame()
b = []
current_max = 0
print("downloading first 100 rows data for contract betdiceadmin")
data = {"pos": str(current_max), "offset": "100", "account_name" : "betdiceadmin"}    
request = requests.post(" https://eos.greymass.com/v1/history/get_actions", json=data)   
print(request)
jsonObj = request.json()
df = pd.DataFrame(json_normalize(jsonObj['actions']))
print("finished downloding rows " + str(current_max) +  " to " + str(max(df.account_action_seq)))
b.append(df)
current_max +=100


while max(df.account_action_seq) >= current_max:    
    print("current maximum is "+str(max(df.account_action_seq)))
    time.sleep(5)
    data = {"pos": str(current_max+1), "offset": "99", "account_name" : "betdiceadmin"}
    request = requests.post(" https://eos.greymass.com/v1/history/get_actions", json=data)
    print(request)
    jsonObj = request.json()
    df = pd.DataFrame(json_normalize(jsonObj['actions']))
    current_max +=100
    b.append(df)
    print("max from df is :" + str(max(df.account_action_seq)))


df3 = pd.concat(b, sort=True)

您得到了
JSONDecodeError
,因为您的
50x
响应内容不是JSON。因此,当您得到200时,您应该运行
request.json()
,如果没有,请再试一次并等待更长的时间。顺便说一句,不要在url中添加空间

while max(df.account_action_seq) >= current_max:    
    print("current maximum is "+str(max(df.account_action_seq)))
    time.sleep(5)
    data = {"pos": str(current_max+1), "offset": "99", "account_name" : "betdiceadmin"}
    request = requests.post("https://eos.greymass.com/v1/history/get_actions", json=data , verify=False)
    if request.status_code == 200:
        jsonObj = request.json()
        df = pd.DataFrame(json_normalize(jsonObj['actions']))
        current_max +=100
        b.append(df)
        print("max from df is :" + str(max(df.account_action_seq)))
    else:
        print("try to post again")
        continue