본문 바로가기
공부/파이썬 Python

커리어넷 API 대학정보 불러오기 (feat. Python ElementTree)

by 혼밥맨 2021. 1. 14.
반응형

커리어넷 API 대학정보 불러오기 (feat. Python ElementTree)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import requests
import xml.etree.ElementTree as ET
from openpyxl import Workbook
 
# 전국 대학교 수
TOTAL_COUNT = 429
 
# 커리어넷 대학정보 URL
# 커리어넷 API를 신청해서 URL 생성하기
URL = 'http://www.career.go.kr/cnet/openapi/getOpenApi?apiKey=00896bcdcec7f4872de2cb8e10f&svcType=api&svcCode=SCHOOL&contentType=xml&gubun=univ_list&perPage=429'
 
# API 요청하기
response = requests.get(URL) 
status = response.status_code 
text = response.text        # XML 형태
root = ET.fromstring(response.text)
 
print(text)
 
"""
<dataSearch>
  <content>
    <link>http://www.tienshanschool.org</link>
    <adres>null</adres>
    <schoolName>Tien Shan International School</schoolName>
    <region>해외거주</region>
    <totalCount>264</totalCount>
    <estType>null</estType>
    <seq>240</seq>
  </content>
  <content>
    <link>http://knose.sc.kr/</link>
    <adres>강원도 강릉시 송정길 43 . 강릉오성학교 (송정동)</adres>
    <schoolName>강릉오성학교</schoolName>
    <region>강원도</region>
    <totalCount>264</totalCount>
    <estType>null</estType>
    <seq>64</seq>
  </content>
...
...
"""
 
# root[i][0].text -> <campusName>       # 캠퍼스구분
# root[i][1].text -> <collegeinfourl>   # 대학정보URL
# root[i][2].text -> <schoolType>       # 학교종류
# root[i][3].text -> <link>             # 학교홈페이지링크
# root[i][4].text -> <schoolGubun>      # 학교구분
# root[i][5].text -> <adres>            # 주소
# root[i][6].text -> <schoolName>       # 학교명
# root[i][7].text -> <region>           # 지역
# root[i][9].text -> <estType>          # 설립
 
 
# OpenPyXL 이용해서 전국 대학정보 엑셀에 담기 위한 초기 작업
ABC = ["A1""B1""C1""D1""E1""F1""G1""H1""I1"]
columns = ["캠퍼스구분""대학정보URL""학교종류""학교홈페이지링크""학교구분""주소""학교명""지역""설립"]
 
# 엑셀파일 쓰기
write_wb = Workbook()
 
# Sheet1에 입력
write_ws = write_wb.active
 
# Head Column 만들기
for (alphabet, col) in zip(ABC, columns): 
  write_ws[alphabet] = col
 
#행 단위로 추가
for i in range(TOTAL_COUNT):
  write_ws.append([root[i][0].text, # <campusName>       # 캠퍼스구분
                   root[i][1].text, # <collegeinfourl>   # 대학정보URL
                   root[i][2].text, # <schoolType>       # 학교종류
                   root[i][3].text, # <link>             # 학교홈페이지링크
                   root[i][4].text, # <schoolGubun>      # 학교구분
                   root[i][5].text, # <adres>            # 주소
                   root[i][6].text, # <schoolName>       # 학교명
                   root[i][7].text, # <region>           # 지역
                   root[i][9].text  # <estType>          # 설립
                 ]) 
 
# 파일 저장하기
write_wb.save("School_Info.csv")
cs
반응형

댓글