资本市场情绪收集和分析系统 - 用于用户资产投机参考的MVP版本。该系统能够:
- 📰 自动抓取新闻源数据(Yahoo Finance, Seeking Alpha)
- 🤖 使用NLP模型进行情绪分析
- 📊 提供情绪统计和趋势分析
- 🔍 支持按股票代码查询情绪数据
- 后端: Python 3.11 + FastAPI
- 数据库: PostgreSQL 15
- 缓存: Redis 7
- NLP: Transformers (RoBERTa)
- 前端: HTML + JavaScript (纯静态)
- 容器: Docker + Docker Compose
- Docker 20.10+
- Docker Compose 2.0+
# 1. 克隆项目
git clone <repository-url>
cd market-sentiment-analysis
# 2. 复制环境配置
cp .env.example .env
# 3. 启动所有服务
./dev.sh start- 前端界面: http://localhost
- API文档: http://localhost:8000/docs
- 健康检查: http://localhost:8000/health
# 抓取新闻
./dev.sh fetch-news
# 分析情绪
./dev.sh process-news
# 查看日志
./dev.sh logs
# 停止服务
./dev.sh stopcurl -X POST http://localhost:8000/api/v1/analyze \
-H "Content-Type: application/json" \
-d '{"text": "Apple stock is doing great today!"}'# 获取最新新闻
curl http://localhost:8000/api/v1/news?limit=10
# 按股票代码筛选
curl http://localhost:8000/api/v1/news?symbol=AAPL
# 按情绪筛选
curl http://localhost:8000/api/v1/news?sentiment=positive# 获取某股票的30天情绪统计
curl http://localhost:8000/api/v1/stats/AAPL?days=30
# 计算特定日期的情绪统计
curl -X POST http://localhost:8000/api/v1/stats/AAPL/calculatemarket-sentiment-analysis/
├── app/ # 应用代码
│ ├── __init__.py
│ ├── config.py # 配置管理
│ ├── models.py # 数据库模型
│ ├── database.py # 数据库连接
│ ├── schemas.py # API数据模型
│ ├── crud.py # 数据库操作
│ ├── sentiment.py # 情绪分析模块
│ ├── scraper.py # 新闻爬虫
│ └── main.py # FastAPI应用入口
├── alembic/ # 数据库迁移
│ └── versions/
├── frontend/ # 前端文件
│ └── index.html
├── tests/ # 测试文件
├── docker-compose.yml # Docker编排配置
├── Dockerfile # Docker镜像配置
├── requirements.txt # Python依赖
└── dev.sh # 开发控制脚本
| 端点 | 方法 | 说明 |
|---|---|---|
/api/v1/analyze |
POST | 分析文本情绪 |
/api/v1/news |
GET | 获取新闻列表 |
/api/v1/news/{id} |
GET | 获取单条新闻 |
/api/v1/news |
POST | 创建新闻条目 |
/api/v1/scraper/fetch |
POST | 抓取新闻 |
/api/v1/scraper/process |
POST | 分析情绪 |
/api/v1/stats/{symbol} |
GET | 获取情绪统计 |
/api/v1/stats/{symbol}/calculate |
POST | 计算情绪统计 |
id: 文章IDtitle: 标题content: 内容url: 链接source: 来源published_at: 发布时间sentiment: 情绪标签sentiment_score: 情绪评分
id: 提及IDarticle_id: 文章IDsymbol: 股票代码mention_sentiment: 情绪
id: 统计IDsymbol: 股票代码date: 日期avg_sentiment_score: 平均情绪分trend_24h: 24小时趋势trend_7d: 7天趋势
# 1. 启动依赖服务
docker-compose up -d postgres redis
# 2. 安装Python依赖
pip install -r requirements.txt
# 3. 设置环境变量
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/market_sentiment"
export REDIS_URL="redis://localhost:6379/0"
# 4. 运行迁移
alembic upgrade head
# 5. 启动开发服务器
uvicorn app.main:app --reload --port 8000# 安装测试依赖
pip install -r requirements.txt[dev]
# 运行所有测试
pytest
# 运行单个测试
pytest tests/test_api.py
# 运行带覆盖率
pytest --cov=app --cov-report=html# 创建新迁移
alembic revision --autogenerate -m "description"
# 升级数据库
alembic upgrade head
# 降级数据库
alembic downgrade -1
# 查看迁移历史
alembic history- 在
app/scraper.py中创建新的scraper类 - 继承
NewsScraper基类 - 实现
fetch_articles()方法 - 在
ScraperFactory._scrapers中注册
- 在
app/sentiment.py中配置新模型 - 修改
SENTIMENT_MODEL环境变量
- 在
app/schemas.py中定义数据模型 - 在
app/main.py中添加路由处理函数 - 在
app/crud.py中实现数据库操作
首次运行时,transformers会自动下载模型(约500MB)。下载完成后会被缓存,后续启动会更快。
当前使用的是通用RoBERTa模型。对于金融领域,可以考虑使用专门的金融情绪分析模型:
nlptown/bert-base-multilingual-uncased-sentimentProsusAI/finbert
新闻网站可能有反爬机制。可以考虑:
- 添加请求延迟
- 使用代理IP
- 添加User-Agent轮换
MIT License
欢迎提交Issue和Pull Request!
- Issues: GitHub Issues