首页 > 解决方案 > 使用 PYTHON 和 JSON 传递 API 页面

问题描述

这是我的 API 完整输出 - JSON 文件

    "took": 150,
    "total": 3377,
    "hits": [{
            Extra - not relevant JSON information
        },
        {    
            Extra - not relevant JSON information
        },
        .......

total - 平均帖子总数。

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))

标签: pythonjsonapi

解决方案


推荐阅读