[英]TypeError: list indices must be integers or slices, not str - get from json
I try to get all lattitudes and longtitudes from this json. 我尝试从此json获取所有纬度和经度。
Code: 码:
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()
Error: 错误:
/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
Where do I go wrong? 我哪里出错了?
In advance, thanks for your help and time. 预先感谢您的帮助和时间。
I just post some of the json to show what it looks like. 我只是发布一些json来显示它的样子。 The json looks like this: 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",
},
Full json: https://pastebin.com/qJywTqa1 完整的json: https : //pastebin.com/qJywTqa1
Your URL construction is incorrect. 您的网址构造不正确。 The URL you have built (as shown in the output of your script) is: 您构建的URL(如脚本输出所示)为:
http://live.ksmobile.net/live/getreplayvideos?userid=userid%3D=735890904669618176&page_size=1000
Where you actually want this: 您实际想要的位置:
http://live.ksmobile.net/live/getreplayvideos?userid=735890904669618176&page_size=1000
So your were actually getting this JSON in your response: 因此,您实际上在响应中得到了此JSON:
{
"status": "200",
"msg": "",
"data": []
}
Which is why you were seeing that error. 这就是为什么您看到该错误的原因。
Here is the corrected script: 这是更正的脚本:
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()
According to your posted json, you have problem in this statement- 根据您发布的json,您在此声明中有问题-
print(coordinates['lat'], coordinates['lnt'])
Here coordinates
is a list having only one item which is dictionary. 在此, coordinates
是仅包含一项的字典的列表。 So your statement should be- 因此,您的声明应为-
print(coordinates[0]['lat'], coordinates[0]['lnt'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.