LSTM 股票预测 Django Web 系统:Python 3.10 + Scrapy 数据采集实战(附 3 大模块源码)

LSTM 股票预测 Django Web 系统:Python 3.10 + Scrapy 数据采集实战(附 3 大模块源码) LSTM股票预测Django Web系统Python 3.10全栈开发实战1. 系统架构设计现代金融科技应用中将机器学习模型产品化为Web服务已成为核心需求。本系统采用三层架构设计数据层Scrapy爬虫MySQL实现高频数据采集算法层PyTorch构建的LSTM时序预测模型表现层DjangoECharts实现交互式可视化关键技术栈版本Python 3.10 Django 4.2 Scrapy 2.11 PyTorch 2.0 ECharts 5.4提示建议使用conda创建隔离环境避免依赖冲突conda create -n stock_pred python3.10 conda activate stock_pred2. 数据采集模块实现2.1 Scrapy爬虫工程化股票数据采集需要处理反爬机制和异常高频请求。我们采用分布式爬虫架构# spiders/stock_spider.py import scrapy from scrapy_redis.spiders import RedisSpider class StockSpider(RedisSpider): name stock redis_key stock:start_urls custom_settings { DOWNLOAD_DELAY: 0.5, CONCURRENT_REQUESTS: 4, ITEM_PIPELINES: { pipelines.StockPipeline: 300 } } def parse(self, response): # 解析股票数据页面 item { code: response.css(.stock-code::text).get(), name: response.css(.stock-name::text).get(), price: float(response.css(.current-price::text).get()), volume: int(response.css(.trade-volume::text).get().replace(,,)) } yield item关键优化点使用Redis实现请求去重和分布式调度自定义User-Agent轮询异常重试机制配置2.2 数据存储设计MySQL表结构设计需考虑时序数据特性CREATE TABLE stock_daily ( id bigint NOT NULL AUTO_INCREMENT, code varchar(10) COLLATE utf8mb4_bin NOT NULL, trade_date date NOT NULL, open decimal(10,2) DEFAULT NULL, high decimal(10,2) DEFAULT NULL, low decimal(10,2) DEFAULT NULL, close decimal(10,2) DEFAULT NULL, volume bigint DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY idx_code_date (code,trade_date) ) ENGINEInnoDB DEFAULT CHARSETutf8mb4 COLLATEutf8mb4_bin;3. LSTM模型开发3.1 数据预处理流程金融时序数据需要特殊处理# utils/data_processor.py import numpy as np from sklearn.preprocessing import MinMaxScaler class StockScaler: def __init__(self, feature_range(-1, 1)): self.scaler MinMaxScaler(feature_range) def fit_transform(self, df): 归一化处理 self._save_meta(df) scaled self.scaler.fit_transform(df.values) return pd.DataFrame(scaled, columnsdf.columns) def inverse_transform(self, data): 反归一化 return self.scaler.inverse_transform(data) def _save_meta(self, df): 保存原始统计量用于还原 self.meta { min: df.min(), max: df.max(), mean: df.mean() }3.2 PyTorch模型实现改进的LSTM架构加入Attention机制# models/predictor.py import torch import torch.nn as nn class LSTMAttention(nn.Module): def __init__(self, input_size5, hidden_size64): super().__init__() self.lstm nn.LSTM(input_size, hidden_size, batch_firstTrue) self.attention nn.Sequential( nn.Linear(hidden_size, hidden_size), nn.Tanh(), nn.Linear(hidden_size, 1) ) self.regressor nn.Linear(hidden_size, 1) def forward(self, x): lstm_out, _ self.lstm(x) # [batch, seq_len, hidden] # Attention机制 attn_weights torch.softmax( self.attention(lstm_out), dim1 ) context torch.sum(attn_weights * lstm_out, dim1) return self.regressor(context)训练关键参数配置# config/train_config.py TRAIN_PARAMS { batch_size: 64, epochs: 100, learning_rate: 0.001, lookback_window: 20, # 使用20天历史数据 train_ratio: 0.8, early_stop_patience: 10 }4. Django系统集成4.1 项目结构设计采用分应用模式组织代码stock_project/ ├── core/ # 核心业务逻辑 ├── predictor/ # 预测模型服务 ├── scraper/ # 爬虫管理 ├── static/ │ └── js/ # ECharts脚本 └── templates/ # 前端页面4.2 异步任务处理使用Celery处理耗时操作# core/tasks.py from celery import shared_task from predictor.inference import make_prediction shared_task(bindTrue) def predict_stock_task(self, stock_code): try: result make_prediction(stock_code) return { status: SUCCESS, result: result.tolist() } except Exception as e: self.retry(exce, countdown60)4.3 前后端数据交互Django视图处理预测请求# core/views.py from django.http import JsonResponse from core.tasks import predict_stock_task def get_prediction(request, stock_code): task predict_stock_task.delay(stock_code) return JsonResponse({task_id: task.id}, status202)前端通过AJAX获取结果// static/js/prediction.js function fetchPrediction(stockCode) { fetch(/api/predict/${stockCode}) .then(response response.json()) .then(data { if (data.status SUCCESS) { renderChart(data.result); } else { checkTaskStatus(data.task_id); } }); }5. ECharts可视化实现5.1 趋势对比图表配置项示例option { tooltip: { trigger: axis, axisPointer: { type: cross } }, legend: { data: [实际价格, 预测价格] }, xAxis: { type: category, data: dates // 日期数组 }, yAxis: { type: value }, series: [ { name: 实际价格, type: line, smooth: true, data: actualPrices }, { name: 预测价格, type: line, smooth: true, lineStyle: { type: dashed }, data: predictedPrices } ] };5.2 移动端适配方案通过响应式设计确保多端兼容/* static/css/responsive.css */ media (max-width: 768px) { .chart-container { width: 100%; height: 300px; } .data-table { font-size: 0.8rem; } }6. 系统部署方案6.1 生产环境配置推荐使用Docker Compose编排服务# docker-compose.prod.yml version: 3.8 services: web: build: . command: gunicorn core.wsgi:application --bind 0.0.0.0:8000 volumes: - static:/app/static depends_on: - redis - mysql celery: build: . command: celery -A core worker -l info depends_on: - redis redis: image: redis:6 ports: - 6379:6379 mysql: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: ${DB_PASSWORD} volumes: - db_data:/var/lib/mysql volumes: static: db_data:6.2 性能优化建议针对高频预测场景的优化策略缓存层对历史预测结果使用Redis缓存模型量化将PyTorch模型转为TorchScript提升推理速度异步加载前端实现数据分片加载CDN加速静态资源部署到CDN7. 扩展开发方向本系统可进一步扩展的功能模块多模型集成结合Transformer等新型架构实时预警设置价格波动阈值通知组合分析多股票相关性分析自动化回测策略历史表现验证实际开发中发现LSTM模型在5-7天的短期预测中表现最佳而更长周期的预测需要引入宏观经济指标作为外部变量。系统源码中已包含模型再训练的接口设计方便后续迭代升级。