Json 接收类型错误:';非类型';对象在Python3中不可随机下标
我正在用Python编写一个简单的程序,它迭代各种API JSON链接,并比较每个数据源的值。在循环浏览其中一个JSON文件时,我调用了一个函数,该函数根据初始JSON文件的一个值调用不同的JSON文件 以下是我的Python代码:Json 接收类型错误:';非类型';对象在Python3中不可随机下标,json,python-3.x,dictionary,Json,Python 3.x,Dictionary,我正在用Python编写一个简单的程序,它迭代各种API JSON链接,并比较每个数据源的值。在循环浏览其中一个JSON文件时,我调用了一个函数,该函数根据初始JSON文件的一个值调用不同的JSON文件 以下是我的Python代码: import requests import json import urllib.request as request from urllib.request import Request, urlopen import time # THINGS ''' in
import requests
import json
import urllib.request as request
from urllib.request import Request, urlopen
import time
# THINGS
'''
international API: https://arsonwarehouse.com/api/v1/foreign-stock
domestic API: https://api.torn.com/market/ ?selections=itemmarket&key=KEY
personal API: https://api.torn.com/user/2519609?selections=money&key=KEY
'''
capacity = 21
moneyOverride = False
totalCost: int
totalIncome: int
#Takes in the cost per item, multiplies by capacity, returns true if it's affordable
def checkAfford(costPer):
global totalCost
totalCost = capacity * costPer
if totalCost < money:
return True
else:
return False
#Puts together the API link with the ID number and calls pullJSON()
def checkDomestic(id):
jsonPULL = pullJSON(f"https://api.torn.com/market/{id}?selections=itemmarket&key=YET")
if jsonPULL == None:
print("error")
else:
return jsonPULL
#Requests the JSON from the server
def pullJSON(url):
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
with urlopen(req) as response:
if response.getcode() == 200:
source = response.read()
data = json.loads(source)
return data
else:
print('An error occurred while attempting to retrieve data from the API.')
internationalData = pullJSON("https://arsonwarehouse.com/api/v1/foreign-stock")
personalData = pullJSON("https://api.torn.com/user/2519609?selections=money&key=YET")
domesticData = checkDomestic(10)
#Money override
if moneyOverride == False:
money = personalData.get("money_onhand")
else:
money = 1000000000000000
print("onhand:", money)
i = 0
length = len(internationalData["items"])
print(length)
for x in internationalData["items"]:
time.sleep(0.5)
ident = x["item_id"]
domPrice = x["price"]
print("DOMESTIC: ID:", ident, " at price:", domPrice)
domestic = checkDomestic(ident).get("itemmarket")[1]
inPrice = domestic["cost"]
if inPrice == None:
print("error", domestic["ID"])
location = x["country"]
print("INTERNATIONAL: ID:", ident, " at price:", inPrice, "location: ", location)
print("end")
这就是internationalData
的样子:
{
"items": [
{
"item_id": 4,
"country": "South Africa",
"price": 750,
"stock": 247,
"reported_at": "2020-06-03T11:47:56Z"
},
{
"item_id": 8,
"country": "Mexico",
"price": 4200,
"stock": 59,
"reported_at": "2020-06-03T11:45:21Z"
}
]
}
我到处寻找答案,但大多数问题都与.sort()
方法有关。我还应该提到,这有时在前10次迭代中随机发生,但不会在最后发生,所以这不是一个超出范围的错误。整个JSON结构保持不变
一个有点相关的问题:我以前在Golang完成过这个项目,我注意到在Go中它比Python完成得快得多。是否有某种冗余使它花费了这么长时间?我知道我有time.sleep()
函数,它是用来确保它不是因为过度使用API而导致的
任何建议都会非常有用
{
"items": [
{
"item_id": 4,
"country": "South Africa",
"price": 750,
"stock": 247,
"reported_at": "2020-06-03T11:47:56Z"
},
{
"item_id": 8,
"country": "Mexico",
"price": 4200,
"stock": 59,
"reported_at": "2020-06-03T11:45:21Z"
}
]
}