본문 바로가기

Python_Intermediate/Json

JSON - URL HTTP Connection Basics(HTTP 접속 기초)

AnKiWoong 2019. 5. 17. 16:15
(追記) (追記ここまで)
반응형
(追記) (追記ここまで)

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


반응형
(追記) (追記ここまで)

DESIGN BY TISTORY 관리자
Total = 852,520명 | Today = 0명 | Yesterday = 70명

티스토리툴바

AltStyle によって変換されたページ (->オリジナル) /