Python

实用模块

requests

优雅简单的 HTTP 模块。

import requests

# GET 请求
r = requests.get('https://api.example.com/data')
print(r.json())

# POST 请求
r = requests.post('https://api.example.com/submit', json={'key': 'value'})
print(r.status_code)

# 带参数的请求
r = requests.get('https://api.example.com/search', params={'q': 'keyword', 'page': 1})

# 设置超时和 headers
r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)

BeautifulSoup

很好用的 HTML/XML 解析器。

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_content, 'html.parser')

# 查找元素
title = soup.find('title')
links = soup.find_all('a')

# CSS 选择器
items = soup.select('.content > p')

json

JSON 编码解码器。

应用举例:

base64

方便地进行 base64 编解码的模块。

应用举例:

argparse

命令行参数解析模块。

import argparse

parser = argparse.ArgumentParser(description='程序说明')
parser.add_argument('-f', '--file', help='输入文件')
parser.add_argument('-v', '--verbose', action='store_true', help='详细输出')
args = parser.parse_args()

logging

Python 标准日志模块。

import logging

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s [%(levelname)s] %(message)s',
    handlers=[
        logging.FileHandler('app.log'),
        logging.StreamHandler()
    ]
)

logging.info('程序启动')
logging.error('发生错误: %s', error)

subprocess

调用外部命令的模块。

import subprocess

# 简单调用
result = subprocess.run(['ls', '-la'], capture_output=True, text=True)
print(result.stdout)

# 管道操作
p1 = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', 'python'], stdin=p1.stdout, stdout=subprocess.PIPE)

常用资源