오늘은 오픈 API에 공개되어 있는 예금자보호 금융상품 조회 서비스 API 와 우체국금융 보험상품정보 조회 서비스 API를 활용해서 시중에서 거래되고 있는 보험 상품 정보들을 스크래핑하는 스크립트를 간단히 소개해드리고자 합니다. 인슈어테크 관련 해커톤에 나갔다가 수상은 못했지만, 여러모로 쓸모있을 거 같아 블로그를 통해 공유합니다.

 

예금자보호 금융상품 조회 서비스 API 와 우체국금융 보험상품정보 조회 서비스 API를 활용해서 시중 보험 상품 정보 스크래핑

사용한 오픈 API 정보

 

#-*- coding:utf-8 -*
import requests
import json

#update datas from openapi
def getPostProduct():
  keyword = ""
  productList = []
  resultList = []
  postProductJson = {"getProductList": ""}
  key = "<<set key>>"
  url = "http://apis.data.go.kr/1721301/KpostInsuranceProductView/insuranceGoods?serviceKey="+ key +"&GOOD_ABNM=" + keyword

  response = requests.get(url)
  jsonData = json.loads(response.content.decode(encoding='utf-8'))
  groupData = jsonData["response"]["body"]["items"]["item"]
  for data in groupData:
    resultList.append({"prtName":data["GOOD_ABNM"], "item":data})

  postProductJson["getProductList"] = resultList
  with open("postProduct.json", 'w', encoding='utf8') as outfile:
    json.dump(postProductJson, outfile, ensure_ascii=False)

def getNotCompany():
  key = "<<set key>>"
  rows = "10000"
  type = "json"
  pTypeList = ["금융투자", "은행", "저축은행"]
  notCompanyList = ["우리종합금융㈜"]
  for pType in pTypeList:
    url = "http://apis.data.go.kr/B190017/service/GetInsuredProductService/getCompanyList?serviceKey=" + key + "&numOfRows=" + rows + "&resultType=" + type + "&regnNm=" + pType

    response = requests.get(url)
    jsonData = json.loads(response.content.decode(encoding='utf-8'))
    groupData = jsonData['getCompanyList']['item'] 

    for data in groupData:
      company = data['fncIstNm']
      notCompanyList.append(company)

  return notCompanyList

def getCompany():
  key = "<<set key>>"
  rows = "10000"
  type = "json"
  url = "http://apis.data.go.kr/B190017/service/GetInsuredProductService/getCompanyList?serviceKey=" + key + "&numOfRows=" + rows + "&resultType=" + type
  response = requests.get(url)
  jsonData = json.loads(response.content.decode(encoding='utf-8'))
  groupData = jsonData['getCompanyList']['item']

  notCompanyList = getNotCompany()
  companyList = []
  for data in groupData:
    company = data['fncIstNm']
    check = str(company in notCompanyList)
    if check == "False":
      companyList.append(company)
  return companyList

def getProduct(company):
  key = "<<set key>>"
  rows = "10000"
  type = "json"
  url = "http://apis.data.go.kr/B190017/service/GetInsuredProductService/getProductList?serviceKey=" + key + "&numOfRows=" + rows + "&resultType=" + type + "&fncIstNm=" + company
  productList = []
  
  response = requests.get(url)
  jsonData = json.loads(response.content.decode(encoding='utf-8'))
  groupData = jsonData['getProductList']['item']
  for data in groupData:
    if data['prdSalDscnDt'] == "":
      productList.append(data['prdNm'])
  return productList
  
if __name__ == "__main__":
  getPostProduct()
  productJson = {"getProductList": ""}
  resultList = []

  companyList = getCompany()
  for company in companyList:
    productList = getProduct(company)
    result = {"company": company, "item": productList}
    resultList.append(result)
  
  productJson["getProductList"] = resultList
  with open("product.json", 'w', encoding='utf8') as outfile:
    json.dump(productJson, outfile, ensure_ascii=False)

 

스크립트 실행 후, 총 2개의 파일 생성

postProduct - 우체국 보험 상품 정보

postProduct.json
json viewer로 본 postProduct.json

product - 예금자 보호 금융 상품 중, 증권/은행 상품을 제외한 보험 상품 정보

product.json
json viewer로 본 product.json

 

반응형

+ Recent posts