Requests: The Most Popular HTTP Library in Python

Requests: The Most Popular HTTP Library in Python!

The Requests library is the king of handling HTTP requests in Python, greatly simplifying the complexity of network requests.

In our daily lives, HTTP requests are involved in everything from browsing the web, logging into accounts, obtaining weather information, querying stock data, to interacting with various Web APIs.

With its simple API and powerful features, the Requests library has become the preferred tool for Python developers to handle network requests. It supports various HTTP methods, handles complex authentication, and elegantly manages common needs such as JSON data handling and file uploads.

1. Installation Method

pip install requests

2. Basic Usage

1. Sending Basic Requests

import requests

# GET request
response = requests.get('https://api.example.com/data')

# POST request
response = requests.post('https://api.example.com/submit', data={'key': 'value'})

# Check response content
print(response.text)
print(response.status_code)

2. Request Parameters and Header Settings

# URL parameters
params = {'key1': 'value1', 'key2': 'value2'}
response = requests.get('https://api.example.com/search', params=params)

# Custom headers
headers = {
    'User-Agent': 'Mozilla/5.0',
    'Authorization': 'Bearer token123'
}
response = requests.get('https://api.example.com/user', headers=headers)

3. Handling JSON Data

# Sending JSON data
json_data = {'name': 'John', 'age': 30}
response = requests.post('https://api.example.com/user', json=json_data)

# Receiving JSON response
data = response.json()

4. File Upload and Download

# Uploading a file
files = {'file': open('document.pdf', 'rb')}
response = requests.post('https://api.example.com/upload', files=files)

# Downloading a file
response = requests.get('https://example.com/image.jpg', stream=True)
with open('image.jpg', 'wb') as f:
    for chunk in response.iter_content(chunk_size=8192):
        f.write(chunk)

3. Advanced Usage

import requests
from requests.auth import HTTPBasicAuth
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

# Session management
session = requests.Session()
session.auth = HTTPBasicAuth('user', 'pass')
session.headers.update({'Authorization': 'Bearer token123'})

# Retry mechanism
retry_strategy = Retry(
    total=3,
    backoff_factor=1,
    status_forcelist=[500, 502, 503, 504]
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session.mount('http://', adapter)
session.mount('https://', adapter)

# Timeout settings
try:
    response = requests.get('https://api.example.com', timeout=(3, 5))
except requests.Timeout:
    print("Request timed out")

4. Practical Application Scenarios

Here is a practical application case of a weather query API:

import requests
import json
from datetime import datetime
import time

class WeatherAPI:
    def __init__(self, api_key):
        self.api_key = api_key
        self.base_url = "https://api.weatherapi.com/v1"
        self.session = self._create_session()

    def _create_session(self):
        session = requests.Session()
        retry_strategy = Retry(
            total=3,
            backoff_factor=0.5,
            status_forcelist=[500, 502, 503, 504]
        )
        adapter = HTTPAdapter(max_retries=retry_strategy)
        session.mount('http://', adapter)
        session.mount('https://', adapter)
        return session

    def get_weather(self, city):
        """Get weather information for a city"""
        try:
            params = {
                'key': self.api_key,
                'q': city,
                'aqi': 'yes'
            }
            response = self.session.get(
                f"{self.base_url}/current.json",
                params=params,
                timeout=5
            )
            response.raise_for_status()
            return self._parse_weather_data(response.json())
        except requests.RequestException as e:
            print(f"Failed to get weather data: {str(e)}")
            return None

    def _parse_weather_data(self, data):
        """Parse weather data"""
        if not data:
            return None
        return {
            'location': data['location']['name'],
            'temperature': data['current']['temp_c'],
            'condition': data['current']['condition']['text'],
            'humidity': data['current']['humidity'],
            'wind_speed': data['current']['wind_kph'],
            'air_quality': data['current'].get('air_quality', {}).get('pm2_5')
        }

# Usage example
weather_api = WeatherAPI('your_api_key')
weather_info = weather_api.get_weather('Beijing')
print(json.dumps(weather_info, indent=2, ensure_ascii=False))

Conclusion

The strength of the Requests library lies in its simplicity and ability to handle various complex network request scenarios. Through the features introduced in this article, we can easily implement API calls, web scraping, file transfers, and more. In practical development, Requests is widely used in web crawling, API integration, and automated testing.

Have you encountered scenarios in your development work that require handling HTTP requests? Or are you interested in how to build more robust network request programs? Feel free to share your experiences, and we can discuss more advanced application techniques of Requests together. If you have questions about a specific network request scenario, let me know, and we can delve into specific solutions.

Would you like me to explain or break down the code?

Leave a Comment