1. request 모듈 사용
import requests
2. 객체 생성
import requests
response = requests.get('URL')
3. 객체 설명
response.status_code : 결과 코드를 갖고 있는 변수.(200 - 정상 / 404 - Page Not Found / 500 - 서버에러)
response.reason : 에러 메시지를 갖는 변수
response.encoding : "utf-8" / "enc-kr" 등을 할당하여 text 속성값의 인코딩을 설정
response.text requests : 모듈이 URL에 접속하여 가져온 내용 전문
4. Sample URL : https://api.androidhive.info/contacts
5. Sample Code
import requests
url = 'https://api.androidhive.info/contacts'
response = requests.get(url)
print(response)
<Response [200]>
Process finished with exit code 0
6. HTTP Code에 따라 프로그램 종료 Code
import requests
url = 'https://api.androidhive.info/contacts'
response = requests.get(url)
if response.status_code != 200:
print("[%d Error] %s" % (response.status_code, response.reason))
quit()
print(response)
<Response [200]>
Process finished with exit code 0
7. 인코딩 형식 지정 출력
import requests
url = 'https://api.androidhive.info/contacts'
response = requests.get(url)
if response.status_code != 200:
print("[%d Error] %s" % (response.status_code, response.reason))
quit()
response.encoding = 'UTF-8'
print(response.text)
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c202",
"name": "Leonardo Dicaprio",
"email": "leonardo_dicaprio@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c203",
"name": "John Wayne",
"email": "john_wayne@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c204",
"name": "Angelina Jolie",
"email": "angelina_jolie@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c205",
"name": "Dido",
"email": "dido@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c206",
"name": "Adele",
"email": "adele@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c207",
"name": "Hugh Jackman",
"email": "hugh_jackman@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c208",
"name": "Will Smith",
"email": "will_smith@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c209",
"name": "Clint Eastwood",
"email": "clint_eastwood@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2010",
"name": "Barack Obama",
"email": "barack_obama@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2011",
"name": "Kate Winslet",
"email": "kate_winslet@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2012",
"name": "Eminem",
"email": "eminem@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
Process finished with exit code 0
'Python_Intermediate > Json' 카테고리의 다른 글
JSON - Basic 예제 (0) | 2019.08.20 |
---|---|
JSON - Data Frame Conversion(Pandas 분석) (0) | 2019.05.17 |
JSON - Site JSON data analysis(URL JSON 분석) (0) | 2019.05.17 |
JSON - Types of Data Types / Basic Syntax(데이터 형식의 종류 / 기본 문법) (0) | 2019.05.17 |