[Python] requests 패키지

Python

2023. 2. 14.

JavaScrpit의 fetch API처럼 서버에 요청을 날리고 응답을 받아올 때 사용하는 라이브러리.

 

기본 내장 라이브러리가 아니기 때문에, 설치부터 해줘야한다.

pip install reqests

 

기본 사용법. 요청을 날려보자.

import requests

res = requests.get( url);

fetch API와 사용법도 아주 유사하다.

 

요청을 날릴 때 다양한 request type을 사용할 수 있다.

r = requests.get('https://api.github.com/events')
r = requests.post('https://httpbin.org/post', data={'key': 'value'})
r = requests.put('https://httpbin.org/put', data={'key': 'value'})
r = requests.delete('https://httpbin.org/delete')
r = requests.head('https://httpbin.org/get')
r = requests.options('https://httpbin.org/get')

 

(예시는 공식 문서에서 가져온 것이다.)

 

응답 결과를 확인 할 수 있다.

r.status_code

 

다양한 응답 데이터 타입을 처리할 수 있다.

r = requests.get('https://api.github.com/events')
r.text
r.content
r.json()
r.raw

 

사용법 공식 문서

https://docs.python-requests.org/en/latest/user/quickstart/

 

Quickstart — Requests 2.28.2 documentation

Eager to get started? This page gives a good introduction in how to get started with Requests. Let’s get started with some simple examples. Passing Parameters In URLs You often want to send some sort of data in the URL’s query string. If you were const

docs.python-requests.org

 

예제까지 잘 설명해주신 한글 문서

https://light-tree.tistory.com/6

 

파이썬(python) Requests 사용법 정리

Requests 는 파이썬에서 HTTP를 사용하기 위해 쓰여지는 라이브러리로, 기본 내장 라이브러리는 아니지만 거의 표준처럼 널리 쓰이고 있다. 개발 과정에서 많이 쓸법한 내용들만 뽑아서 정리했다.

light-tree.tistory.com

 

'Python' 카테고리의 다른 글

[Python] BeautifulSoup 패키지  (0) 2023.02.14