import requests
import json
import csv
import pandas
#有header的API调用并写入文件
url = 'https://imi.rhonda.ai/api/job/?search=United&page_size=40'
headers = {'Authorization': 'Token 111bb68520a5f7fe63fbd85325118c5f25e56d9e'}
r = requests.get(url, headers=headers)
f1 = open('/Users/hao/Desktop/oldfile.json', "w")
f1.write(r.text)
f1.close()
#读取文件 保存部分内容进另一文件
f1 = open('/Users/hao/Desktop/oldfile.json', "r")
f2 = open('/Users/hao/Desktop/newfile.json', "w")
start = False
count = 0
while True:
c = f1.read(1)
if not c:
break
if (c == "["):
start = True
count = count + 1
if (c == "]"):
count = count - 1
if (True == start):
f2.write(c)
if (0 == count):
break
f2.close()
f1.close()
#讲JSON转换成csv 使用pandas包
f2 = open('/Users/hao/Desktop/newfile.json', "r")
f3 = open('/Users/hao/Desktop/newfile.csv', "w")
df = pandas.read_json(f2)
df.to_csv(f3)
f3.close()
f2.close()