Requests: An Elegant HTTP Request Library for Python

In today’s internet era, web services are ubiquitous, and the exchange and retrieval of data have become crucial. Whether it’s fetching remote API data, submitting forms, downloading files, or developing web crawlers, dealing with the HTTP protocol is essential. The Python <span>Requests</span> library serves as an elegant HTTP request library for Python, providing developers with a simple, intuitive, and powerful API that makes HTTP requests easy and enjoyable.

Requests: An Elegant HTTP Request Library for Python

1. Introduction to the Library and Its Practical Applications

<span>Requests</span> is the most popular HTTP client library in Python, built on top of <span>urllib3</span>, providing a higher-level and more user-friendly API interface. Compared to the <span>urllib</span> in the Python standard library, <span>Requests</span> has cleaner code, is easier to use, and offers richer functionality. In practical life, <span>Requests</span> has a wide range of applications. For example, developers can use it to build API clients to fetch weather data, stock information, social media content, etc.; they can develop web crawlers to automatically scrape web content; and it can also be used for automated testing to simulate user requests to a web server and verify whether the server’s response meets expectations.

2. Installing the Library

<span>Requests</span> can be installed using Python’s package management tool <span>pip</span>. Execute the following command in the command line:

pip install requests

Once installed, you can import the <span>Requests</span> library in your Python code to verify:

import requests
print(requests.__version__)

3. Basic Usage

1. Sending a GET Request

import requests
# Send a GET request
response = requests.get('https://api.github.com/users/octocat')
# Check the response status code
if response.status_code == 200:
    # Get JSON formatted response data
    data = response.json()
    print(f"Username: {data['login']}")
    print(f"Followers: {data['followers']}")
else:
    print(f"Request failed, status code: {response.status_code}")

2. Sending a GET Request with Parameters

import requests
# Define request parameters
params = {
    'q': 'python',
    'page': 1}
# Send a GET request with parameters
response = requests.get('https://api.github.com/search/repositories', params=params)
if response.status_code == 200:
    data = response.json()
    print(f"Found {data['total_count']} repositories")
    for item in data['items'][:3]:
        print(f"- {item['name']}: {item['html_url']}")

3. Sending a POST Request

import requests
# Define request data
data = {
    'username': 'testuser',
    'password': 'testpass'}
# Send a POST request
response = requests.post('https://example.com/login', data=data)
print(f"Response status code: {response.status_code}")
print(f"Response content: {response.text}")

4. Handling Response Headers and Body

import requests
response = requests.get('https://www.example.com')
# Get response headers
print("Response headers:")
for key, value in response.headers.items():
    print(f"{key}: {value}")
# Get response text content
print("\nResponse content:")
print(response.text[:200] + "...")  # Display the first 200 characters

4. Advanced Usage

1. Session Persistence

import requests
# Create a session object
session = requests.Session()
# Login request (assuming login is required)
login_data = {
    'username': 'user',
    'password': 'pass'}
session.post('https://example.com/login', data=login_data)
# Use the same session to send subsequent requests
response = session.get('https://example.com/dashboard')
print(response.text)

2. Timeout Settings and Exception Handling

import requests
try:
    # Set timeout to 5 seconds
    response = requests.get('https://example.com', timeout=5)
    response.raise_for_status()  # Check response status code
except requests.exceptions.Timeout:
    print("Request timed out")
except requests.exceptions.HTTPError as e:
    print(f"HTTP error: {e}")
except requests.exceptions.RequestException as e:
    print(f"Request exception: {e}")

3. File Upload

import requests
# Prepare file
files = {'file': open('example.txt', 'rb')}
# Send file upload request
response = requests.post('https://example.com/upload', files=files)
print(response.status_code)

5. Practical Application Scenarios

1. Weather Query Application

Develop a weather query application that retrieves weather information for a specified city by calling a weather API and displays it to the user.

2. Data Collection and Analysis

Build a web crawler that periodically scrapes data from websites, cleans and analyzes it to support decision-making.

<span>Requests</span> library, with its simple API and powerful features, has become the preferred tool for Python developers to make HTTP requests. It not only makes the code more elegant but also improves development efficiency. Have you encountered any interesting projects or challenges while using the <span>Requests</span> library? Feel free to share your experiences and stories.

Below is a deep case code that implements a simple command-line translation tool, calling the Baidu Translation API:

import requests
import json
import hashlib
import random
import sys
class Translator:
    def __init__(self, appid, secret_key):
        self.appid = appid
        self.secret_key = secret_key
        self.api_url = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
    def translate(self, text, from_lang='auto', to_lang='zh'):
        # Generate random number
        salt = random.randint(32768, 65536)
        # Construct signature
        sign = self.appid + text + str(salt) + self.secret_key
        sign = hashlib.md5(sign.encode()).hexdigest()
        # Prepare request parameters
        params = {
            'q': text,
            'from': from_lang,
            'to': to_lang,
            'appid': self.appid,
            'salt': salt,
            'sign': sign
        }
        # Send request
        try:
            response = requests.get(self.api_url, params=params)
            response.raise_for_status()
            result = response.json()
            # Process API return result
            if 'trans_result' in result:
                return '\n'.join([item['dst'] for item in result['trans_result']])
            else:
                return f"Translation failed: {result.get('error_msg', 'Unknown error')}"
        except requests.exceptions.RequestException as e:
            return f"Request exception: {e}"
def main():
    # Please replace with your own Baidu Translation API appid and secret_key
    appid = 'your_appid'
    secret_key = 'your_secret_key'
    if appid == 'your_appid' or secret_key == 'your_secret_key':
        print("Please fill in your Baidu Translation API appid and secret_key in the code first")
        return
    translator = Translator(appid, secret_key)
    print("Welcome to the command-line translation tool (enter 'q' to exit)")
    while True:
        text = input("Please enter the text to be translated: ").strip()
        if text.lower() == 'q':
            break
        if not text:
            continue
        # Default is English to Chinese, input 2 to switch to Chinese to English
        mode = input("Translation mode (default English -> Chinese, input 2 to switch to Chinese -> English): ").strip()
        from_lang, to_lang = ('en', 'zh') if mode != '2' else ('zh', 'en')
        result = translator.translate(text, from_lang, to_lang)
        print(f"Translation result:\n{result}\n")
if __name__ == "__main__":
    main()

This case implements a command-line translation tool based on the Baidu Translation API, supporting mutual translation between Chinese and English, including the complete request signature generation, API calling, and result processing flow.

Leave a Comment