# 공공데이터 사용하기

## HTTP 통신

python에서 데이터 통신하기 위해 여러 통신 라이브러리가 있습니다.\
우리는 requests 모듈을 사용합니다.

```
import requests
URL = "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo?ServiceKey=DkRPUL4om5zMDGR7jLAde9a%2Fsyg%2Bc%2FPKtilNgDwDjDZbJPzbuConyIOEYYZS96MjHndan5JKGCuZ8Kfm5HRyzw%3D%3D&_type=json&solYear=2018&solMonth=05"
html = requests.get(URL)
print(html.text)
```

01  데이터 통신을 하기위해 requests 모듈을 가져옵니다.\
02  공공데이터 중 공휴일 정보를 가져오는 API\
03  requests.get(URL) URL을 호출해서 데이터를 가져옵니다.

![](/files/-LQn2jeG-DrjHrCgvwuZ)

html을 출력하면 데이터가 보이지 않습니다.\
html에서 받아온 데이터는 서버에서 보내주는 형태로 받아야 합니다.\
서버에서 보내는 형태와 상관없이 사용할 수 있는 방법과 json을 받는 방법을 알려드리겠습니다.

```
import requests
URL = "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo?solYear=2018&solMonth=05&ServiceKey=DkRPUL4om5zMDGR7jLAde9a%2Fsyg%2Bc%2FPKtilNgDwDjDZbJPzbuConyIOEYYZS96MjHndan5JKGCuZ8Kfm5HRyzw%3D%3D&_type=json"
html = requests.get(URL)
print(html.text)
print('-----------------------')
print(html.json())
```

![](/files/-LQn3ZO63a-DycRY6UqE)

결과를 보면 html.text, html.json()이 둘다 같아 보입니다.\
자세히 보면 html.text는 " " 를 이용되었고, json()은 ' '를 사용했네요. \
더 큰 차이가 있습니다.&#x20;

### response 키의 값을 가져오기

```
import requests
URL = "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo?solYear=2018&solMonth=05&ServiceKey=DkRPUL4om5zMDGR7jLAde9a%2Fsyg%2Bc%2FPKtilNgDwDjDZbJPzbuConyIOEYYZS96MjHndan5JKGCuZ8Kfm5HRyzw%3D%3D&_type=json"
html = requests.get(URL)
print(html.text.get('response'))
print('-----------------------')
print(html.json().get('response'))
```

04  딕셔너리 형태로 보이는 값에서 response 만 가져오려는 시도\
06  4번과 같음

결과적으로 4번 라인에서 에러가 발생합니다.\
text 형태이기 때문에 .get을 사용할 수 없습니다.\
딕셔너리 형태의 데이터를 활용하기 위해서는 .json() 이후 사용하셔야 합니다.

### 첫 번 째 공휴일 정보 가져오는 코드&#x20;

```
import requests
URL = "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo?solYear=2018&solMonth=05&ServiceKey=DkRPUL4om5zMDGR7jLAde9a%2Fsyg%2Bc%2FPKtilNgDwDjDZbJPzbuConyIOEYYZS96MjHndan5JKGCuZ8Kfm5HRyzw%3D%3D&_type=json"
html = requests.get(URL)
first = html.json().get('response').get('body').get('items').get('item')[0]
print(first.get('dateKind'))
print(first.get('dateName'))
print(first.get('isHoliday'))
print(first.get('locdate'))
```

04  리스트로 되어있는 item key의 0번째만 가져와서 사


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://study-code.gitbook.io/python-basic/open-api/undefined.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
