Guidelines

requests 라이브러리 핡심 정리

requestsλŠ” νŒŒμ΄μ¬μ—μ„œ HTTP μš”μ²­κ³Ό 응닡을 μ²˜λ¦¬ν•˜λŠ” λŒ€ν‘œμ μΈ 라이브러리둜, requestsλŠ” μ›Ή 크둀링, API(Application Programming Interface) μš”μ²­ λ“± λ‹€μ–‘ν•œ μ›Ή μš”μ²­μ„ 보내고 응닡을 μ²˜λ¦¬ν•  λ•Œ μ‚¬μš©λ©λ‹ˆλ‹€.

μ°Έκ³ : APIλŠ” μ„œλ²„μ™€ ν΄λΌμ΄μ–ΈνŠΈ μ‚¬μ΄μ˜ 톡신 κ·œμΉ™μ„ μ •μ˜ν•œ μΈν„°νŽ˜μ΄μŠ€λ₯Ό λœ»ν•©λ‹ˆλ‹€.


requests의 νŠΉμ§•

  1. κ°„νŽΈν•œ μ‚¬μš©λ²•: 직관적인 API둜 HTTP μš”μ²­κ³Ό 응닡을 κ°„λ‹¨ν•˜κ²Œ μ²˜λ¦¬ν•©λ‹ˆλ‹€.

  2. λ‹€μ–‘ν•œ HTTP λ©”μ†Œλ“œ 지원: GET(쑰회), POST(생성), PUT(μˆ˜μ •), DELETE(μ‚­μ œ) λ“± λ‹€μ–‘ν•œ HTTP λ©”μ†Œλ“œλ₯Ό μ§€μ›ν•©λ‹ˆλ‹€.

  3. νŽΈλ¦¬ν•œ 응닡 처리: 응닡 객체λ₯Ό 톡해 μƒνƒœ μ½”λ“œ, ν…μŠ€νŠΈ, JSON λ“±μ˜ 응닡 데이터λ₯Ό μ‰½κ²Œ μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.


GET μš”μ²­

μ›Ή νŽ˜μ΄μ§€ λ˜λŠ” API둜 데이터λ₯Ό μš”μ²­(Request)ν•  λ•ŒλŠ” requests.get() ν•¨μˆ˜λ‘œ GET μš”μ²­μ„ λ³΄λƒ…λ‹ˆλ‹€.

requests GET μš”μ²­ 예제
import requests # μ›Ή νŽ˜μ΄μ§€μ— GET μš”μ²­μ„ 보내고 응닡 λ°›κΈ° response = requests.get('https://example.com') print(response.text) # μ‘λ‹΅μœΌλ‘œ 받은 HTML 좜λ ₯

POST μš”μ²­

μ„œλ²„μ— 데이터λ₯Ό λ³΄λ‚΄κ±°λ‚˜ νŠΉμ • μž‘μ—…μ„ μš”μ²­ν•  λ•ŒλŠ” requests.post() ν•¨μˆ˜λ‘œ POST μš”μ²­μ„ λ³΄λƒ…λ‹ˆλ‹€.

requests POST μš”μ²­ 예제
# μ„œλ²„μ— POST μš”μ²­ 보내기 payload = {'key1': 'value1', 'key2': 'value2'} response = requests.post('https://httpbin.org/post', data=payload) res = response.text # 응닡을 res λ³€μˆ˜μ— μ €μž₯ print(res)

응닡 객체 닀루기

  • response.status_code: HTTP μƒνƒœ μ½”λ“œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€ (예: 200, 404).

  • response.headers: 응닡 헀더λ₯Ό λ”•μ…”λ„ˆλ¦¬ ν˜•νƒœλ‘œ λ°˜ν™˜ν•©λ‹ˆλ‹€.

  • response.json(): JSON 응닡을 파이썬 객체둜 λ³€ν™˜ν•©λ‹ˆλ‹€.


μ‹€μŠ΅

ν™”λ©΄ 였λ₯Έμͺ½ μ½”λ“œ μ‹€ν–‰ λ²„νŠΌμ„ λˆ„λ₯΄κ³ , 크둀링 κ²°κ³Όλ₯Ό ν™•μΈν•˜κ±°λ‚˜ μ½”λ“œλ₯Ό μˆ˜μ •ν•΄ λ³΄μ„Έμš”!

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result