• 회원가입
  • 로그인
  • 구글아이디로 로그인

[module] Python - requests.get() 메서드 - 지정 URL에 GET 요청 보냄. (= get메서드 = 겟메서드)

4123  

목차

  1. requests.get() 예제
  2. requests.get() 정의
  3. requests.get() 구문
  4. requests.get() 매개변수

 

실습 준비 상태. 


1. 

https://homzzang.com/python/demo.php 내용

홈짱닷컴 Homzzang.com

<?php echo $_GET['lang']; ?>

 

2.

http 모드로 접속 시, https로 자동 리디렉션 중임.

 

 

requests.get() 예제

 

import requests

x = requests.get('https://homzzang.com')

print(x.status_code) # 200

print(x.text) # 소스 코드

 

 

requests.get() 정의

 

지정 URL에 get 요청 보냄.

 

 

requests.get() 구문

 

requests.get(url, params={key: value}, args)

 


[매개변수]

 

url

필수. 요청 url 주소

 

args

선택. 아래 매개변수 중  0개 이상 사용 가능.

 


[반환값]

 

requests.Response 객체 반환.

 

 

requests.get() 매개변수

url 

필수. 요청 URL

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

x = requests.get(url)

print(x.text)

 


params

선택. 쿼리 문자열로 보낼 Dictionary, Tuple이나 Bytes 목록.

※ 기본값: None

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

x = requests.get(url, params = {"lang": "CSS"})

print(x.text)

 


allow_redirects 

선택. 리디렉션 허용 여부 선택하는 Boolean.

※ True : 허용. (기본값)

※ False : 비허용. 

 

(예제)

import requests 

url = 'http://homzzang.com/python/demo.php'

# http로 요청해도 https로 가도록 리디렉션 허용.

x = requests.get(url, allow_redirects=True)

print(x.text)

 

결과값: 홈짱닷컴 Homzzang.com 


auth

선택. 특정 HTTP 인증을 활성화하는 Tuple.

※ 기본값: None

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

HTTP 기본 인증으로 요청 보냄.

x = requests.get(url, auth = ('user', 'pass')) 

print(x.status_code) # 200 (※ 통신 성공 의미.)



cert

선택. 인증서 파일 또는 키를 지정하는 String 또는 Tuple.

※ 기본값: None

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

클라이언트 측 인증서로 사용할 인증서 지정.

x = requests.get(url, cert='cert/hz.cert')

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


cookies

선택. 지정 URL로 보낼 쿠키 Dictionary.

※ 기본값: None

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

# 서버에 쿠키 전송

x = requests.get(url, cookies = {"color": "red"})

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


headers

선택. 지정 URL로 보낼 HTTP 헤더 Dictionary.

※ 기본값: None

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

# HTTP 헤더 설정.

x = requests.get(url, headers = {"HTTP_HOST": "홈짱닷컴"})

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


proxies

선택. 프록시 URL에 대한 프로토콜 Dictionary.

※ 기본값: None

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

# 무료 프록시 주소 찾아 해당 프록시 통해 요청 보냄.

x = requests.get(url, proxies = { "https" : "https://1.1.0.1:80"})

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


stream

선택. 응답 스트리밍 허용 여부 Boolean.

※ False : 허용 X (즉, 다운로드). (기본값)

※ True : 허용 O. (즉, 스트리밍).

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

# 스트리밍 허용.

x = requests.get(url, stream=True)

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


timeout

선택. 클라이언트가 연결 (그리고/또는) 응답을 보낼 때까지 기다리는 시간 (초) 나타내는 Number 또는 Tuple.

※ 기본값: None. (즉, 연결 닫힐 때까지 요청 계속됨 의미.)

 

(예제)

import requests

url = 'https://homzzang.com/python/demo.php'

# timeout 설명 위해, 연결 시간 초과되게 매우 짧게 시간 설정.

x = requests.get(url, timeout=0.001)

print(x.status_code) # 200 (※ 통신 성공 의미.)

 


verify

선택. 서버 TLS 인증서 확인하는 Boolean 또는 문자열 표시.

※ True : 확인. (기본값)

※ False : 비확인.

 

(예제1)

import requests

url = 'https://homzzang.com/python/demo.php'

# TLS 인증서 경로 사용해 요청.

x = requests.get(url, verify='cert/tlscertificate')

print(x.status_code) # 200 (※ 통신 성공 의미.)

 

(예제2)

import requests

url = 'https://homzzang.com/python/demo.php'

# TLS 인증서 경로 사용 않고 요청.

x = requests.get(url, verify=False)

print(x.status_code) # 200 (※ 통신 성공 의미.)

 



분류 제목
module Python - random 모듈 메서드 종류
module Python - random.seed() 메서드 - 난수 생성기 초기화 (= seed메서드 = 시드)
module Python - random.getstate() 메서드 - 난수 생성기 현재 상태 반환. (= getstat…
module Python - random.setstate() 메서드 - 난수 생성기 상태 복원. (= setstate메서…
module Python - random.getrandbits() 메서드 ★ - 지정 bit 크기의 정수 반환. (= g…
module Python - random.randrange() 메서드 ★★ - 지정 범위 안에서 정수형 난수 반환. (=…
module Python - random.randint() 메서드 ★★ - 지정 범위 안 int형 난수 생성. (= ra…
module Python - random.choice() 메서드 ★★ - 요소 랜덤 반환. (= choice메서드 = 초…
module Python - random.choices() 메서드 - 가중치 반영해 랜덤 요소 반환. (= choices…
module Python - random.shuffle() 메서드 ★★ - 요소 순서 뒤섞기. (= 순서 랜덤 = shu…
module Python - random.sample() 메서드 ★ - 일부 요소 랜덤 선택. (= sample메서드 =…
module Python - random.random() 메서드 ★ - 0 ~ 1 사이 부동소수 랜덤 반환. (= ran…
module Python - random.uniform() 메서드 ★ - 지정 범위 안 랜덤 부동소수 반환. (= uni…
module Python - random.triangular() 메서드 - 지정 범위 안 가중치 반영 랜덤 부동소수 반환…
module Python - random.betavariate() 메서드 △ - 베타분포 (통계용) 기반 0~1 사이 랜…
module Python - random.expovariate() 메서드 △ - 지수분포 (통계용) 기반 랜덤 부동소수 …
module Python - random.gammavariate() 메서드 △ - 감마분포 (통계용) 기반 랜덤 부동소수…
module Python - random.gauss() 메서드 △ - 가우스분포 (확률이론용) 기반 랜덤 부동소수 반환.
module Python - random.lognormvariate() 메서드 △ - 로그정규분포 (확률이론용) 기반 랜…
module Python - random.normalvariate() 메서드 △ - 정규분포 (확률이론용) 기반 랜덤 …
1/7
목록
찾아주셔서 감사합니다. Since 2012