python - 使用 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))
解决方案
推荐阅读
- python - 如何在 python 中使用 while(i = getNext()) 模式
- c++ - 如何创建优化的图形多色记录器?
- java - Java - 将 String[] 数组的所有值转换/复制到 ArrayList
- python - Tweepy 流媒体非常慢
- swift - 我无法在自定义数组类型中添加元素?
- amazon-web-services - aws s3 CLI 命令提前退出批处理文件
- python - 多处理进程 grpc 通信异步回调 StatusCode.UNAVAILABLE
- hex - 我需要将轨迹栏值转换为十六进制字节并将其显示在文本框中
- java - return 语句在返回类型不是 void 但抛出异常的方法中是可选的吗?
- javascript - 如何通过 HTML 中的查询字符串更改 url 重定向