简体   繁体   English

在Python中抓取json时出现“字符串索引必须为整数”错误

[英]“string indices must be integers” error when scraping json in Python

So I'm trying to grab some data from a website. 因此,我试图从网站上获取一些数据。 The url is 该网址是

https://rbx.rocks/apis/item?id=1285307

So right now i got 所以现在我得到了

print requests.get('https://rbx.rocks/apis/item?id=1285307').json()[0]['stats']

If ran, it gives the error, "string indices must be integers" 如果运行,则会出现错误,“字符串索引必须为整数”

I'm currently running python 2.7 我目前正在运行python 2.7

You need to parse the JSON object. 您需要解析JSON对象。

add json.loads() as: 将json.loads()添加为:

import requests
import json

print json.loads(requests.get('https://rbx.rocks/apis/item?id=1285307').json())['stats']

To identify the source of error, you could perform "one operation at a time", and get details on the result, eg 为了确定错误的来源,您可以“一次执行一次操作”,并获得有关结果的详细信息,例如

a = requests.get('https://rbx.rocks/apis/item?id=1285307')
print(a, type(a))
b = a.json()
print(b, type(b))
c = b[0]
print(c, type(c))
d = c['stats']
print(d, type(d))

This is clearly not the solution, but: 这显然不是解决方案,而是:

  1. It may help. 这可能会有所帮助。
  2. It does not fit in a comment. 它不适合发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:使用 Python 解析 Json 时,字符串索引必须是整数错误 - TypeError: string indices must be integers error when parsing Json with Python Python 和 JSON 错误 - TypeError:字符串索引必须是整数 - Python and JSON Error - TypeError: string indices must be integers python和json,错误-TypeError:字符串索引必须为整数 - python and json, Error - TypeError: string indices must be integers Python:JSON标准化“字符串索引必须为整数”错误 - Python: json normalize “String indices must be integers” error Python for loop json.loads错误字符串索引必须为整数 - Python for loop json.loads error string indices must be integers Python Json TypeError:字符串索引必须是整数错误 - Python Json TypeError: string indices must be integers error 在使用 python “字符串索引必须是整数”的 JSON 导入中出现错误 - Getting an error in JSON Import with python “string indices must be integers” TypeError:通过Python读取JSON时,字符串索引必须为整数 - TypeError: string indices must be integers when read JSON via Python python pandas-解析JSON时发生TypeError:字符串索引必须为整数 - python pandas - TypeError when parsing JSON: string indices must be integers TypeError:在 python 中使用 json 文件时,字符串索引必须是整数 - TypeError: string indices must be integers when using json file in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM