非常适合输出为系列教程、技术博客或课程讲义的标题!下面是以《Python爬虫(54)Python数据治理全攻略:从爬虫清洗到NLP情感分析的实战演进》为核心的完整内容框架,包括爬虫采集、数据清洗、治理、存储与NLP情感分析的实战路径。
🐍 Python爬虫(54)Python数据治理全攻略
—— 从爬虫清洗到NLP情感分析的实战演进
🧭 本期目标
在这一篇中,我们将完成一整套从网页采集 → 数据清洗 → 治理规则设定 → 入库管理 → 情感分析的完整闭环流程,为你打通数据智能的全链条路径。
🪝 第一阶段:爬虫采集
✅ 技术栈:
requests
/aiohttp
(同步/异步请求)BeautifulSoup
/lxml
(DOM解析)Selenium
/Playwright
(动态页面抓取)
📌 示例:爬取豆瓣影评数据
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0'}
url = 'https://movie.douban.com/subject/3541415/comments?start=0&limit=20'
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
comments = [c.get_text(strip=True) for c in soup.select('.comment span.short')]
🧹 第二阶段:数据清洗
✅ 技术点:
- 重复去除、空值过滤
- 正则提取、分词预处理
- 特殊字符去除、统一格式
📌 示例:去除 HTML、表情符号、数字等杂质
import re
def clean_comment(text):
text = re.sub(r'[^\u4e00-\u9fa5a-zA-Z]', ' ', text)
return ' '.join(text.split())
cleaned = [clean_comment(c) for c in comments]
🧪 第三阶段:数据治理与规则设定
✅ 数据治理目标:
- 结构统一、质量保障
- 自定义治理规则(如敏感词、极端长度、情绪倾向等)
📌 示例:治理规则示意
def is_valid_comment(text):
if len(text) < 5 or len(text) > 300:
return False
if any(word in text for word in ['广告', '代写', '代评']):
return False
return True
governed_comments = list(filter(is_valid_comment, cleaned))
🛢️ 第四阶段:存储与管理
✅ 存储方式选择:
- 小型测试:
CSV
/SQLite
- 中型:
MongoDB
/MySQL
- 大规模:
Elasticsearch
/Hive
/ClickHouse
📌 示例:保存入 SQLite
import sqlite3
conn = sqlite3.connect('douban.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS comments (id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT)")
cursor.executemany("INSERT INTO comments(text) VALUES(?)", [(c,) for c in governed_comments])
conn.commit()
conn.close()
🤖 第五阶段:NLP 情感分析
✅ 技术栈推荐:
snownlp
(中文简易情感分析)bert4torch
/transformers
(深度学习)jieba
+ 词向量 + SVM/LightGBM(机器学习自建)
📌 示例:用 SnowNLP 快速情感打分
from snownlp import SnowNLP
results = [{'text': c, 'score': SnowNLP(c).sentiments} for c in governed_comments]
🎯 输出结构化情感数据:
for r in results:
print(f"评分:{r['score']:.2f},评论内容:{r['text']}")
📊 第六阶段:分析 + 可视化
- 情感倾向分布图:
matplotlib
/seaborn
- 情绪词云图:
wordcloud
- 评论热度时序趋势:
pandas + matplotlib
import matplotlib.pyplot as plt
scores = [r['score'] for r in results]
plt.hist(scores, bins=20, color='skyblue')
plt.title("情感分布图")
plt.xlabel("情绪分值(0-负面,1-正面)")
plt.ylabel("评论数量")
plt.show()
✅ 总结:数据治理一体化思路
阶段 | 工具 | 目标 |
---|---|---|
爬取 | requests、Selenium | 高质量原始数据采集 |
清洗 | re、pandas | 格式统一、降噪 |
治理 | 自定义规则、敏感词库 | 数据可信、安全可控 |
存储 | SQLite、MySQL、MongoDB | 可维护结构化管理 |
分析 | SnowNLP、transformers | 情感洞察、结构输出 |
可视化 | matplotlib、wordcloud | 结果呈现、可解释性提升 |
📦 BONUS:项目实战演进建议
- ✅ 加入定时任务(如 Airflow/Celery 实现自动抓取)
- ✅ 加入异常监控/日志系统(如 Sentry)
- ✅ 改用 ChatGLM/Qwen 等模型进行评论自动总结/分群
- ✅ 情感标签加入用户画像推荐系统中
✅ 一、项目结构目录(工程标准)
python-data-governance-nlp/
├── data/
│ └── raw/ # 原始抓取数据
│ └── cleaned/ # 清洗后的数据
├── db/
│ └── comments.db # SQLite数据库
├── notebooks/
│ └── main_analysis.ipynb # 主分析流程脚本
├── scripts/
│ ├── crawler.py # 爬虫采集模块
│ ├── cleaner.py # 数据清洗与治理模块
│ ├── sentiment.py # 情感分析模块
│ ├── visualizer.py # 可视化模块
├── requirements.txt
├── README.md
✅ 二、requirements.txt
依赖
requests
beautifulsoup4
snownlp
pandas
matplotlib
wordcloud
sqlite3
✅ 三、核心脚本功能预览
1. crawler.py
(采集模块)
import requests
from bs4 import BeautifulSoup
def fetch_comments(start=0, limit=20):
url = f"https://movie.douban.com/subject/3541415/comments?start={start}&limit={limit}"
headers = {'User-Agent': 'Mozilla/5.0'}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
comments = [c.get_text(strip=True) for c in soup.select('.comment span.short')]
return comments
2. cleaner.py
(清洗 + 治理)
import re
def clean_text(text):
text = re.sub(r'[^\u4e00-\u9fa5a-zA-Z]', ' ', text)
return ' '.join(text.split())
def filter_comment(text):
if len(text) < 5 or len(text) > 300:
return False
if any(w in text for w in ['广告', '代评', '代写']):
return False
return True
3. sentiment.py
(情感打分)
from snownlp import SnowNLP
def score_comment(text):
return SnowNLP(text).sentiments
4. visualizer.py
(可视化模块)
import matplotlib.pyplot as plt
from wordcloud import WordCloud
def plot_sentiment_distribution(scores):
plt.hist(scores, bins=20, color='skyblue')
plt.title("情绪分布图")
plt.xlabel("情感分数(越高越正面)")
plt.ylabel("评论数量")
plt.show()
def generate_wordcloud(text_list):
text = ' '.join(text_list)
wc = WordCloud(font_path='simhei.ttf', width=800, height=400).generate(text)
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.title("评论词云图")
plt.show()
✅ 四、主 Jupyter 脚本 main_analysis.ipynb
# 1. 爬取数据
from scripts.crawler import fetch_comments
comments = fetch_comments(start=0, limit=100)
# 2. 清洗和治理
from scripts.cleaner import clean_text, filter_comment
cleaned = [clean_text(c) for c in comments]
governed = list(filter(filter_comment, cleaned))
# 3. 情感分析
from scripts.sentiment import score_comment
results = [{'text': c, 'score': score_comment(c)} for c in governed]
# 4. 可视化
from scripts.visualizer import plot_sentiment_distribution, generate_wordcloud
plot_sentiment_distribution([r['score'] for r in results])
generate_wordcloud([r['text'] for r in results])
✅ 五、扩展建议
目标 | 推荐做法 |
---|---|
定时运行 | 配合 schedule / APScheduler |
存储进数据库 | 使用 sqlite3 或 SQLAlchemy |
多平台抓取 | 抽象出 URL 适配器 |
使用大模型总结 | 集成 transformers + ChatGLM/Qwen |
部署 API | 用 FastAPI 快速构建 REST 接口 |
发表回复