본문 바로가기
공부/파이썬 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
"""커리어넷 API 이용해서 직업정보 불러오기"""
# 필요한 라이브러리 import
import requests
import xml.etree.ElementTree as ET
# 직업정보를 엑셀에 저장
from openpyxl import Workbook
 
TOTAL_COUNT = 454
 
# 커리어넷 API에서 생성한 URL
URL = 'http://www.career.go.kr/cnet/openapi/getOpenApi?apiKey=01839636bcdc7f4872de3cb9g10efc&svcType=api&svcCode=JOB&contentType=xml&gubun=job_dic_list&perPage=454' 
 
response = requests.get(URL) 
status = response.status_code 
text = response.text #XML 형태 텍스트로 저장
root = ET.fromstring(response.text)
 
# root[i][0].text -> <profession>      # 직업분야
# root[i][1].text -> <summary>         # 직업설명
# root[i][2].text -> <similarJob>      # 유사직업
# root[i][3].text -> <salery>          # 연봉 (salery 오타가 아니고 커리어넷 tag에 실제로 salery라고 돼있음)
# root[i][4].text -> <jobdicSeq>       # 직업코드ID
# root[i][5].text -> <equalemployment> # 고용평등
# root[i][7].text -> <aptd_type_code>  # 적성유형별코드
# root[i][8].text -> <prospect>        # 일자리전망
# root[i][9].text -> <job_ctg_code>    # 직업분류코드
# root[i][10].text -> <job_code>       # 직업코드
# root[i][11].text -> <job>            # 직업
# root[i][12].text -> <possibility>    # 발전가능성 
 
ABC = ["A1""B1""C1""D1""E1""F1""G1""H1""I1""J1","K1""L1"]
columns = ["직업분야""직업설명""유사직업""연봉""직업코드ID""고용평등""적성유형별코드""일자리전망""직업분류코드" ,"직업코드""직업""발전가능성"]
 
# Sheet1에 입력
write_wb = Workbook()
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,  # <profession>      # 직업분야
                   root[i][1].text,  # <summary>         # 직업설명
                   root[i][2].text,  # <similarJob>      # 유사직업
                   root[i][3].text,  # <salery>          # 연봉
                   root[i][4].text,  # <jobdicSeq>       # 직업코드ID
                   root[i][5].text,  # <equalemployment> # 고용평등
                   root[i][7].text,  # <aptd_type_code>  # 적성유형별코드
                   root[i][8].text,  # <prospect>        # 일자리전망
                   root[i][9].text,  # <job_ctg_code>    # 직업분류코드
                   root[i][10].text, # <job_code>       # 직업코드
                   root[i][11].text, # <job>            # 직업
                   root[i][12].text  # <possibility>    # 발전가능성 
                   ])
 
 
write_wb.save("Job_Info.csv")
 
 
cs
반응형

댓글