【问题标题】:TypeError: byte indices must be integers or slices, not strTypeError:字节索引必须是整数或切片,而不是 str
【发布时间】:2019-02-05 17:10:38
【问题描述】:
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

你能帮帮我吗?

【问题讨论】:

  • 您的示例和错误消息中的代码不合适(int()str() 转换)-请澄清!
  • "byte indices must be integers or slices, not str" 表示你做了a_bytes_object[a_str_object],其中只有a_bytes_object[an_int_object],而你的代码有request["Sales"],所以很明显@ 987654328@是bytes对象,也就是说不支持["Sales"]

标签: python


【解决方案1】:

这应该可以工作

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

您可以阅读反序列化 here

【讨论】:

    【解决方案2】:

    希望对你有帮助……

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 2015-12-09
      • 2021-11-28
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多