Python Requests Library: Installation, Basic Usage, and Advanced Scenarios
This article introduces the Python Requests library, explains how to install it, demonstrates basic HTTP methods such as GET and POST with code examples, and explores advanced use‑cases like handling JSON, binary data, custom headers, cookies, sessions, SSL verification, proxies, timeouts, and authentication.
Introduction Requests is a simple yet powerful Python HTTP library that supports Python 2.7 and Python 3+, making HTTP requests easy and readable.
Installation pip install requests Basic request methods Requests provides convenient functions for the common HTTP verbs:
GET: requests.get('http://httpbin.org/get') POST: requests.post('http://httpbin.org/post') PUT: requests.put('http://httpbin.org/put') DELETE: requests.delete('http://httpbin.org/delete') HEAD: requests.head('http://httpbin.org/get') OPTIONS: requests.options('http://httpbin.org/get') GET without parameters
import requests
response = requests.get('http://httpbin.org/get')
print(response.text)GET with query parameters
import requests
response = requests.get('http://httpbin.org/get?name=tom&age=25')
print(response.text)GET using the params argument
import requests
data = {'name': 'tom', 'age': 25}
response = requests.get('http://httpbin.org/get', params=data)
print(response.text)Parsing JSON responses
import requests, json
response = requests.get('http://httpbin.org/get')
print(type(response.text))
print(response.json())
print(json.loads(response.text))
print(type(response.json()))Downloading binary data
import requests
response = requests.get('https://www.baidu.com/favicon.ico')
print(type(response.text), type(response.content))
print(response.text)
print(response.content)Custom headers
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'}
response = requests.get('https://www.qq.com/', headers=headers)
print(response.text)POST requests (form data)
import requests
data = {'name': 'tom', 'age': '25'}
response = requests.post('http://httpbin.org/post', data=data)
print(response.text)POST requests (JSON payload)
import requests
data = {'name': 'tom', 'age': '25'}
response = requests.post('http://httpbin.org/post', json=data)
print(response.text)Response attributes
import requests
response = requests.get('http://www.qq.com')
print(type(response.status_code), response.status_code)
print(type(response.headers), response.headers)
print(type(response.cookies), response.cookies)
print(type(response.url), response.url)
print(type(response.history), response.history)Common HTTP status codes A comprehensive list of status‑code constants (e.g., 200 OK, 404 Not Found, 500 Internal Server Error) is shown for reference.
Advanced scenarios
File upload:
import requests
files = {'file': open('vite.png', 'rb')}
response = requests.post('http://httpbin.org/post', files=files)
print(response.text)Retrieving cookies:
import requests
response = requests.get('https://www.zhihu.com')
print(response.cookies)
for key, value in response.cookies.items():
print(key + '=' + value)Session persistence / simulated login:
import requests
r = requests.get('http://www.baidu.com')
print(r.cookies['BDORZ'])
print(tuple(r.cookies))SSL verification bypass:
import requests
from requests.packages import urllib3
urllib3.disable_warnings()
response = requests.get('https://www.12306.cn', verify=False)
print(response.status_code)Proxy configuration:
import requests
proxies = {'http': 'http://127.0.0.1:8888', 'https': 'https://127.0.0.1:6666'}
response = requests.get('https://www.baidu.com', proxies=proxies)
print(response.status_code)Timeout handling:
import requests
from requests.exceptions import ReadTimeout
try:
response = requests.get('http://httpbin.org/get', timeout=0.005)
print(response.status_code)
except ReadTimeout:
print('timeout')Basic authentication:
import requests
from requests.auth import HTTPBasicAuth
response = requests.get('https://passport.baidu.com/v2/api/?login', auth=HTTPBasicAuth('user', '123'))
print(response.status_code)Small case study – maintaining a session
import random, time, datetime, requests
def make_request(session, body):
resp = session.post('https://accounts.douban.com/j/mobile/login/basic', json=body)
print(resp.text)
def main():
session = requests.Session()
start = time.time()
for _ in range(100):
make_request(session, {'admin': '123456'})
end = time.time()
print(f'发送100次请求,耗时:{end - start}')
if __name__ == '__main__':
main()© The content is compiled from online sources; original authors retain copyright.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
