-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
170 lines (152 loc) · 7.35 KB
/
test.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Python 샘플 코드 #
import json
import urllib.request
import urllib.parse
import xmltodict
servicekey = 'oXFNR8BgFm4XU8GWU9ipGvj20Y9fBuvytfINkjq6fASRin0xIYGyO3lUUYQiTMb4%2Fjuno0wZg7azEaby0ZnLag%3D%3D'
url = 'http://apis.data.go.kr/9710000/NationalAssemblyInfoService/'
getlist = 'getMemberCurrStateList' # 국회의원 현황조회 method_num = 0
getdetail = 'getMemberDetailInfoList' # 국회의원 상세조회 method_num = 1
getjungdang = 'getJungDangMemberCurrStateList' # 소속정당별 국회의원 목록조회 method_num = 2
getcomm = 'getCommMemberCurrStateList' # 소속위원회별 국회의원 목록조회 method_num = 3
getmethod = 'getLoOrProporMemberCurrStateList' # 당선방법별 국회의원 목록 정보조회 method_num = 4
getlocalmen = 'getLocalMemberCurrStateList' # 지역별 국회의원 목록 정보조회 method_num = 5
getparty = 'getPolySearch' # 정당검색 method_num = 6
getlocal = 'getLocalSearch' # 지역검색 method_num = 7
getCd = 'getMemberNameInfoList' # 이름검색
"""
dict_type = xmltodict.parse(response_body)
json_type = json.dumps(dict_type)
dict2_type = json.loads(json_type)
items = dict2_type['response']['body']['items']['item']
for item in items:
print(item)
# 이름이랑 num 만 출력
print(item['empNm'], item['num'])
"""
def getitem(method_num):
if method_num == 0 : #list
request = urllib.request.Request(
url + getlist + '?' + 'ServiceKey=oXFNR8BgFm4XU8GWU9ipGvj20Y9fBuvytfINkjq6fASRin0xIYGyO3lUUYQiTMb4%2Fjuno0wZg7azEaby0ZnLag%3D%3D'
+ "&" + "numOfRows=300"
+ "&" + "pageNo=1")
request.get_method = lambda: 'GET'
response_body = urllib.request.urlopen(request).read().decode('utf8')
dict_type = xmltodict.parse(response_body)
json_type = json.dumps(dict_type)
dict2_type = json.loads(json_type)
items0 = dict2_type['response']['body']['items']['item']
return items0
elif method_num == 1 : #detail
request = urllib.request.Request(
url + getdetail + '?' + 'ServiceKey=oXFNR8BgFm4XU8GWU9ipGvj20Y9fBuvytfINkjq6fASRin0xIYGyO3lUUYQiTMb4%2Fjuno0wZg7azEaby0ZnLag%3D%3D'
+ "&" + "numOfRows=300"
+ "&" + "pageNo=1")
request.get_method = lambda: 'GET'
response_body = urllib.request.urlopen(request).read().decode('utf8')
dict_type = xmltodict.parse(response_body)
json_type = json.dumps(dict_type)
dict2_type = json.loads(json_type)
items1 = dict2_type['response']['body']['items']['item']
return items1
elif method_num == 2 : #getjungdang
request = urllib.request.Request(
url + getjungdang + '?' + 'ServiceKey=oXFNR8BgFm4XU8GWU9ipGvj20Y9fBuvytfINkjq6fASRin0xIYGyO3lUUYQiTMb4%2Fjuno0wZg7azEaby0ZnLag%3D%3D'
+ "&" + "numOfRows=300"
+ "&" + "pageNo=1")
request.get_method = lambda: 'GET'
response_body = urllib.request.urlopen(request).read().decode('utf8')
dict_type = xmltodict.parse(response_body)
json_type = json.dumps(dict_type)
dict2_type = json.loads(json_type)
items2 = dict2_type['response']['body']['items']['item']
return items2
elif method_num == 3 : #getcomm
request = urllib.request.Request(
url + getcomm + '?' + 'ServiceKey=oXFNR8BgFm4XU8GWU9ipGvj20Y9fBuvytfINkjq6fASRin0xIYGyO3lUUYQiTMb4%2Fjuno0wZg7azEaby0ZnLag%3D%3D'
+ "&" + "numOfRows=300"
+ "&" + "pageNo=1")
request.get_method = lambda: 'GET'
response_body = urllib.request.urlopen(request).read().decode('utf8')
dict_type = xmltodict.parse(response_body)
json_type = json.dumps(dict_type)
dict2_type = json.loads(json_type)
items3 = dict2_type['response']['body']['items']['item']
return items3
elif method_num == 4 : #getmethod
request = urllib.request.Request(
url + getmethod + '?' + 'ServiceKey=oXFNR8BgFm4XU8GWU9ipGvj20Y9fBuvytfINkjq6fASRin0xIYGyO3lUUYQiTMb4%2Fjuno0wZg7azEaby0ZnLag%3D%3D'
+ "&" + "numOfRows=300"
+ "&" + "pageNo=1")
request.get_method = lambda: 'GET'
response_body = urllib.request.urlopen(request).read().decode('utf8')
dict_type = xmltodict.parse(response_body)
json_type = json.dumps(dict_type)
dict2_type = json.loads(json_type)
items4 = dict2_type['response']['body']['items']['item']
return items4
elif method_num == 5 : #getlocalmen
request = urllib.request.Request(
url + getlocalmen + '?' + 'ServiceKey=oXFNR8BgFm4XU8GWU9ipGvj20Y9fBuvytfINkjq6fASRin0xIYGyO3lUUYQiTMb4%2Fjuno0wZg7azEaby0ZnLag%3D%3D'
+ "&" + "numOfRows=300"
+ "&" + "pageNo=1")
request.get_method = lambda: 'GET'
response_body = urllib.request.urlopen(request).read().decode('utf8')
dict_type = xmltodict.parse(response_body)
json_type = json.dumps(dict_type)
dict2_type = json.loads(json_type)
items5 = dict2_type['response']['body']['items']['item']
return items5
elif method_num == 6 : #getparty
request = urllib.request.Request(
url + getparty + '?' + 'ServiceKey=oXFNR8BgFm4XU8GWU9ipGvj20Y9fBuvytfINkjq6fASRin0xIYGyO3lUUYQiTMb4%2Fjuno0wZg7azEaby0ZnLag%3D%3D'
+ "&" + "numOfRows=300"
+ "&" + "pageNo=1")
request.get_method = lambda: 'GET'
response_body = urllib.request.urlopen(request).read().decode('utf8')
dict_type = xmltodict.parse(response_body)
json_type = json.dumps(dict_type)
dict2_type = json.loads(json_type)
items6 = dict2_type['response']['body']['items']['item']
return items6
elif method_num == 7: # getlocal
request = urllib.request.Request(
url + getlocal + '?' + 'ServiceKey=oXFNR8BgFm4XU8GWU9ipGvj20Y9fBuvytfINkjq6fASRin0xIYGyO3lUUYQiTMb4%2Fjuno0wZg7azEaby0ZnLag%3D%3D'
+ "&" + "numOfRows=300"
+ "&" + "pageNo=1")
request.get_method = lambda: 'GET'
response_body = urllib.request.urlopen(request).read().decode('utf8')
dict_type = xmltodict.parse(response_body)
json_type = json.dumps(dict_type)
dict2_type = json.loads(json_type)
items7 = dict2_type['response']['body']['items']['item']
return items7
else :
print("Method_num is uncorrect!!")
exit(1)
#정당과 취미 검색
def getpartyCd(num, deptCd):
request = urllib.request.Request(
url + getdetail + '?' + 'ServiceKey=oXFNR8BgFm4XU8GWU9ipGvj20Y9fBuvytfINkjq6fASRin0xIYGyO3lUUYQiTMb4%2Fjuno0wZg7azEaby0ZnLag%3D%3D'
+ "&" + "numOfRows=300"
+ "&" + "pageNo=1"
+ "&" + "dept_cd=" + deptCd
+ "&" + "num=" + num)
request.get_method = lambda: 'GET'
response_body = urllib.request.urlopen(request).read().decode('utf8')
dict_type = xmltodict.parse(response_body)
json_type = json.dumps(dict_type)
dict2_type = json.loads(json_type)
item = dict2_type['response']['body']['item']
party = item['polyNm']
try:
hobbyCd = item['hbbyCd']
except KeyError:
hobbyCd = "없음"
return party, hobbyCd
def getCd(name, cnx, cursor):
table = "assemblyman"
select_query = "select assemblymanCd from assemblyman where assemblyman.empNm = '" + name + "';"
cursor.execute(select_query)
Cds = cursor.fetchall()
return Cds[0][0]