TypeError: byte indices must be integers or slices, not str

放肆的年华 提交于 2021-02-20 05:56:56

问题


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

this gives the following error:

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

Can you help me out?


回答1:


This should work

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

You can read up on deserializing here




回答2:


I hope it can help you...

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'])


来源:https://stackoverflow.com/questions/52102398/typeerror-byte-indices-must-be-integers-or-slices-not-str

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!