这是我的API完整输出-JSON文件
"took": 150,
"total": 3377,
"hits": [{
Extra - not relevant JSON information
},
{
Extra - not relevant JSON information
},
.......
总计-平均帖子总数。
API限制为每个请求250个帖子
我需要遍历页面并获取所有帖子,然后写入单个JSON文件。
我所拥有的:
这是我从API中提取数据的方式: def post_puller():
apiUrl = "https://XXXX.XXXX.com/1.0/projects/{}/inbox/search.json".format(config.project_id)
json={
'metrics': ['doc','impression','reach','engagement','repost'],
'flag': {'rt': False},
'focuses': [{'id':XXXXX, 'include': True}],
'from': '{c.date_from}'.format(c=config),
'to': '{c.date_to}'.format(c=config),
'tz': '{c.tz}'.format(c=config),
"start": 0,
'limit': 250
}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {k.token}'.format(k=key)
}
response = requests.post(url = apiUrl, headers=headers, json=json)
result = response.text
with open(config.data, "w+") as op:
op.write(result.encode("utf-8"))
这是我的python代码,可获取数据,对其进行修改并将其写入文件:
def modify_file_emoji(FILE_NAME,OUTPUT):
print("Ready to remove issues from {} file".format(OUTPUT))
jsonFile = open(FILE_NAME, "r")
data = json.load(jsonFile)
jsonFile.close()
#Modify JSON File, that BQ will accept it.
with open(OUTPUT, "w+") as outfileJSON:
outfileJSON.write(json.dumps(data['hits']))
with open(OUTPUT, "r") as infileInsight:
insightData = infileInsight.read()\
.replace("","")\
with open(OUTPUT, "w+") as outfileInsight:
outfileInsight.write(insightData)
print("File: {} modified - removed all the known issues".format(OUTPUT))