Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-从json提取数据时出现问题_Python_Json_Python 3.x_Instagram Api - Fatal编程技术网

Python-从json提取数据时出现问题

Python-从json提取数据时出现问题,python,json,python-3.x,instagram-api,Python,Json,Python 3.x,Instagram Api,要提取Instagram条目上的数据,我需要下载媒体列表,然后分别下载每个条目的数据。 我只是做错了一些事情,因为它只为我获取了一个条目的数据,而不是每个我必须更改才能正确获取的条目的数据 这是目前的代码: import urllib.request as o import json import csv from pandas.io.json import json_normalize import pandas as pd url = 'https://graph.facebook.com

要提取Instagram条目上的数据,我需要下载媒体列表,然后分别下载每个条目的数据。 我只是做错了一些事情,因为它只为我获取了一个条目的数据,而不是每个我必须更改才能正确获取的条目的数据

这是目前的代码:

import urllib.request as o
import json
import csv
from pandas.io.json import json_normalize
import pandas as pd

url = 'https://graph.facebook.com/v3.2/1234567891011/media?fields=media_type,like_count,comments_count,timestamp&limit=500&access_token=xxx'
link1 = 'https://graph.facebook.com/v3.2/'
link2 = '/insights?metric=engagement%2Cimpressions%2Creach%2Csaved&access_token=xxx'
with o.urlopen(url) as jfile :
    data1 = json.load(jfile)
    df = json_normalize(data1["data"])
    linki = []
    for dane3 in df:
        linki = link1 + df['id'] + link2
        dx = []
        with o.urlopen(linki[0]) as file2 :
            data2 = json.load(file2)
            dx = json_normalize(data2["data"],
                              record_path ='values',
                              meta =['id', 'name', 'title'])
            dx['ident'] =dx['id'][0].split("/")[0]
dn7 = dx.pivot(index='ident', columns='name', values='value')
dn7
我要提取的数据是:

ident|engagement|impressions|reach|saved
987654321|65|2142|1943|2

我在使用Python 3的代码中需要改进什么?

在df中dane3的每次迭代中,您都根据当前json响应
数据帧重新分配
dx
。这意味着您只保留与上次后期处理相关的信息

相反,您可以保留一个规范化JSON
DataFrame
s的列表,并在处理完所有帖子后启用它们

您还通过
df['ID']
linki[0]
for
循环的每次迭代中使用相同的帖子ID,这意味着您将只获得第一篇帖子的数据。相反,循环应该迭代
数据帧的
'id'
列的值,即df['id']
中的post_id的

post_data = []
with o.urlopen(url) as jfile:
    data1 = json.load(jfile)
    df = json_normalize(data1["data"])
    for post_id in df['id']:
        linki = link1 + post_id + link2
        with o.urlopen(linki) as file2:
            data2 = json.load(file2)
            dx = json_normalize(data2["data"],
                                record_path ='values',
                                meta =['id', 'name', 'title'])
            dx['ident'] = dx['id'][0].split("/")[0]
            post_data.append(dx)
dn7 = pd.concat(post_data).pivot(index='ident', columns='name', values='value')

非常感谢您提供的信息,但可能不完全是因为在循环中始终关闭相同的结果。对不起,我不确定您的意思是什么?你测试过这个解决方案吗?不幸的是,我不能,因为我没有访问令牌。所以我测试了这个解决方案,其行为与我的类似,只有5次相同。