使用python post request处理错误502

时间:2018-11-10 10:12:27

标签: post python-requests try-catch

试图构建一个简单的脚本来下载具有多个发布请求的数据。 有时由于响应[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)

enter image description here

1 个答案:

答案 0 :(得分:1)

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

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