【问题标题】:TypeError: list indices must be integers or slices, not str - get from jsonTypeError:列表索引必须是整数或切片,而不是 str - 从 json 获取
【发布时间】:2017-07-05 19:58:05
【问题描述】:

我尝试从这个 json 中获取所有纬度和经度。

代码:

import urllib.parse
import requests

raw_json = 'http://live.ksmobile.net/live/getreplayvideos?userid='
print()

userid = 735890904669618176
#userid = input('UserID: ')

url = raw_json + urllib.parse.urlencode({'userid=': userid}) + '&page_size=1000'
print(url)

json_data = requests.get(url).json()
print()


for coordinates in json_data['data']['video_info']:
    print(coordinates['lat'], coordinates['lnt'])
    print()

错误:

/usr/bin/python3.6 /media/anon/3D0B8DD536C9574F/PythonProjects/getLocation/getCoordinates

http://live.ksmobile.net/live/getreplayvideos?userid=userid%3D=735890904669618176&page_size=1000

Traceback (most recent call last):
  File "/media/anon/3D0B8DD536C9574F/PythonProjects/getLocation/getCoordinates", line 17, in <module>
    for coordinates in json_data['data']['video_info']:
TypeError: list indices must be integers or slices, not str

Process finished with exit code 1

我哪里出错了?

提前感谢您的帮助和时间。

我只是发布一些 json 来展示它的样子。 json 看起来像这样:

{
  "status": "200",
  "msg": "",
  "data": {
    "time": "1499275646",
    "video_info": [
      {
        "vid": "14992026438883533757",
        "watchnumber": "38",
        "topicid": "0",
        "topic": "",
        "vtime": "1499202678",
        "title": "happy 4th of july",
        "userid": "735890904669618176",
        "online": "0",           
        "addr": "",
        "isaddr": "2",
        "lnt": "-80.1282576",
        "lat": "26.2810628",
        "area": "A_US",
        "countryCode": "US",
        "chatSystem": "1",
        },

完整的json:https://pastebin.com/qJywTqa1

【问题讨论】:

  • json_data 必须是字典列表。所以第一个[] 参数需要是切片或整数...
  • …或者它是一个字典,但它的组件不是。我相信是这样的。
  • 没有关于您的数据的一些信息,很难说。请在此处发布您的 json 数据。
  • 好的,我刚做了:)
  • 看起来你发布的代码和你运行的代码不一样。回溯指向一行for coordinates in json_data['data']['video_i']: video_i 是什么?它不在您的来源中。

标签: python json python-3.x


【解决方案1】:

您的网址结构不正确。您构建的 URL(如脚本的输出所示)是:

http://live.ksmobile.net/live/getreplayvideos?userid=userid%3D=735890904669618176&page_size=1000

你真正想要的地方:

http://live.ksmobile.net/live/getreplayvideos?userid=735890904669618176&page_size=1000

所以你实际上在你的回复中得到了这个 JSON:

{
  "status": "200",
  "msg": "",
  "data": []
}

这就是您看到该错误的原因。

这是更正后的脚本:

import urllib.parse
import requests

raw_json = 'http://live.ksmobile.net/live/getreplayvideos?'
print()

userid = 735890904669618176
#userid = input('UserID: ')

url = raw_json + urllib.parse.urlencode({'userid': userid}) + '&page_size=1000'
print(url)

json_data = requests.get(url).json()
print()


for coordinates in json_data['data']['video_info']:
    print(coordinates['lat'], coordinates['lnt'])
    print()

【讨论】:

  • 有效!非常感谢! Idk 这是做什么的: import json with open('/tmp/data.json', 'w') as f: json.dump(json_data, f, indent=2) 但也许有一天我会学到这一点。 :)
  • @aquatic7 哎呀,对不起。那是调试代码。我用它将实际响应 JSON 保存到文件中。你不需要它,我已将其从帖子中删除。
【解决方案2】:

根据您发布的 json,您在此声明中有问题-

print(coordinates['lat'], coordinates['lnt'])

这里coordinates 是一个列表,其中只有一个项目是字典。所以你的陈述应该是-

print(coordinates[0]['lat'], coordinates[0]['lnt'])

【讨论】:

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