迭代Instagram json

2024-04-25 15:24:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到一个JSON对象作为对instagramapi调用的响应。你知道吗

我想访问响应中所有图片的URL,到目前为止,我只访问了第一张图片: 你知道吗

from pprint import pprint
with open('test.json') as data_file:
    mydata = json.load(data_file)
    print mydata["data"][0]["images"]["standard_resolution"]["url"]

我正在努力正确地迭代我的数据。你知道吗

json大致如下:

{
"meta:":{}
"data":[
    {}
    {}
    {}
}

我被这个嵌套循环卡住了:

for x in mydata["data"]:
   for y in x:
       print y

输出

attribution
tags
user
comments
filter
images
link
location
created_time
users_in_photo
caption
type
id
likes 

Tags: 对象infromjsonurlfordata图片
2条回答
dataset = mydata['data']
for data in dataset:
   url = data['images']['standard_resolution']['url']

如果您的第一个图像是作为-

mydata["data"][0]["images"]["standard_resolution"]["url"]

然后您应该迭代mydata["data"],这是一个列表,使用for循环并从其中的每个字典中获取每个url。你知道吗

示例-

with open('test.json') as data_file:
    mydata = json.load(data_file)
    for img in mydata["data"]:
        print img["images"]["standard_resolution"]["url"]

相关问题 更多 >