post - 使用 python 发布请求处理错误 502
问题描述
尝试构建一个简单的脚本来下载具有多个发布请求的数据。有时我会因为响应 [504] 而出现错误。
我尝试了一些尝试/除以处理此错误,但不知何故我没有捕捉到事件
我附上了没有异常处理的代码和错误图片。我知道错误来自 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
推荐阅读
- for-loop - (名称变量未定义)
- c# - 具有共享连接的内存数据库
- odoo - 我如何从 odoo13 qweb 报告中的菜单打印报告
- python - 使用带有鉴别器的通用关联时如何在父模型上映射公共多态参数
- postgresql - 用于创建和填充数据的 PostgreSQL 函数在运行后似乎没有做任何事情
- python - 将文件名添加到合并的 csv 文件中
- python - 超过2个人的jaccard相似度
- sql - SQL 用户定义表类型 INTO 语句
- caching - 尝试在流式视频播放的非缓冲(缓存)部分中向前跳过时,Windows Media Player 会给出错误消息
- batch-file - 在优化过程中访问右键菜单