python 오픈 API 데이터 분할 출력하기
조회수 465회
안녕하세요. 오픈API 데이터 출력을 공부하고 있습니다. 그런데 데이터를 몇백개를 가져오는게 아니고 몇천개, 몇만개를 가져올시 메모리 과부하 등 문제가 생기더라구요. 그래서 짤라서 가져와서 합치는 형태로 생각하고있습니다. 그런데 코드를 어떻게 짜야될지 전혀 감이 안잡혀 이렇게 문의드립니다.
코드는 다음과 같습니다.
# 곤충정보
# -*- encoding: utf-8 -*- #ANSI
import requests
from pprint import pprint
import json
import xmltodict
import codecs
import sys
url = "http://openapi.nature.go.kr/openapi/service/rest/InsectService/spcmSearch"
querystring = {"serviceKey":"3w68NZC3y7WLN4yWYQe/0/aQFOD1gwWOn6hJqe30ILw6GDoFiF0/zjloMWbUTtU0zGbZnvL5ob2ZakxK4IiPmg==","numOfRows":"4000"}
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"files\"\r\n\r\ndump.sql\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'cache-control': "no-cache",
'postman-token': "f64c1b55-f465-af25-7491-d49805d01ff7"
}
response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
#print(response.text)
f = open('response.xml', 'w', -10, encoding='utf-8')
f.write(response.text)
f.close()
with open("response.xml", 'r', -10, encoding='utf-8') as f:
xmlString = f.read()
print("xml input (response.xml):")
print(xmlString)
jsonString = json.dumps(xmltodict.parse(xmlString), indent=4)
print("\nJSON output(output.json):")
print(jsonString)
with open("output.json", 'w', -10, encoding='utf-8') as f:
f.write(jsonString)
이렇게 json파일형태로4천개(numOfRows)의 데이터를 가져온다고 했을 때 다음 4천건은 for문을 사용해서 해야되는지.. 알려주시면 감사하겠습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
댓글 입력