TypeError:字节索引必须是整数或切片,而不是str

时间:2018-08-30 17:18:55

标签: python

request = requests.get("http://api.roblox.com/Marketplace/ProductInfo?assetId=1834225941").content
print(str(request["Sales"]))

这会产生以下错误:

Traceback (most recent call last):
  File "C:\Users\yorks\Desktop\BloxUtility\bot.py", line 22, in <module>
    print(int(request["Sales"]))
TypeError: byte indices must be integers or slices, not str

你能帮我吗?

2 个答案:

答案 0 :(得分:0)

这应该有效

import requests
import json
request = requests.get("http://api.roblox.com/Marketplace/ProductInfo?assetId=1834225941").text
a = json.loads(request)
print(a['Sales'])

您可以阅读反序列化here

的内容。

答案 1 :(得分:0)

我希望它可以帮助您...

import json
import requests

response = requests.get("your_api_url")
if (response.status_code != 200):
    print("API is not working. status code :" + str(response.status_code))
else:
    print("API is working. status code :" + str(response.status_code))
    datas = json.loads(response.text)
    for value in datas['data']:
        print(value)
        #print(values['column_name'])