[英]“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: 这显然不是解决方案,而是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.