从《Two Heroes》到代码英雄:用Python爬虫分析可可英语学习笔记的文本情感
2026/6/7 14:07:10 网站建设 项目流程

从《Two Heroes》到代码英雄:用Python爬虫分析可可英语学习笔记的文本情感

当技术遇上人文,数据便有了温度。这篇文章将带你用Python爬取可可英语上的经典课文《Two Heroes for the Price of One》,通过文本分析技术揭示文字背后的情感脉络。不同于传统语言学习,我们将以开发者视角重新解构这篇关于勇气与牺牲的英文故事。

1. 环境准备与目标设定

在开始前,我们需要明确分析目标:通过量化统计文中表达不同情感的词汇出现频率,客观呈现文章的"情感指纹"。这需要三个关键技术组件:

  • 网页抓取:获取可可英语的课文原文及翻译
  • 文本处理:清洗数据并提取特征词汇
  • 情感分析:建立情感分类模型或使用现有词典

推荐使用以下工具栈:

# 核心依赖库 import requests from bs4 import BeautifulSoup import jieba # 中文分词 from nltk.sentiment import SentimentIntensityAnalyzer import pandas as pd

提示:NLTK的情感分析模块需要额外下载词典数据,可通过nltk.download('vader_lexicon')获取

2. 网页抓取实战

可可英语的课文页面结构相对规整,我们可以定位到正文所在的HTML元素。以下是通过开发者工具分析后的抓取策略:

def fetch_keenglish_content(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' } response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') # 定位正文区域 content_div = soup.find('div', class_='article-content') paragraphs = [p.get_text(strip=True) for p in content_div.find_all('p')] return '\n'.join(paragraphs)

典型页面元素结构如下表所示:

元素选择器内容类型示例
.article-title课文标题Two Heroes for the Price of One
.article-content p正文段落When I saw her on the Good Morning...
.translation p中文翻译当她出现在《早安美国》访谈节目中...

3. 文本预处理关键技术

原始文本需要经过多个处理步骤才能用于分析:

  1. 语言分离:区分英文原文和中文翻译
  2. 停用词过滤:移除无意义的常见词汇
  3. 词形还原:将单词还原为基本形式

对于英文处理,NLTK提供了完整的工作流:

from nltk.corpus import stopwords from nltk.stem import WordNetLemmatizer def preprocess_text(text): # 分词 words = word_tokenize(text.lower()) # 移除停用词 stops = set(stopwords.words('english')) words = [w for w in words if w not in stops] # 词形还原 lemmatizer = WordNetLemmatizer() return [lemmatizer.lemmatize(w) for w in words]

中文处理则需要不同的策略:

def process_chinese(text): words = jieba.lcut(text) # 加载中文停用词表 with open('chinese_stopwords.txt') as f: stops = set(f.read().splitlines()) return [w for w in words if w not in stops]

4. 情感分析实现

我们采用两种互补的分析方法:

4.1 基于词典的方法

NLTK的VADER情感分析工具特别适合社交媒体和短文分析:

analyzer = SentimentIntensityAnalyzer() sample_text = "Her face still registered that awful sadness" scores = analyzer.polarity_scores(sample_text) # 输出: {'neg': 0.636, 'neu': 0.364, 'pos': 0.0, 'compound': -0.5423}

4.2 自定义情感分类

针对课文特点,我们可以建立专属的情感词汇表:

emotion_categories = { 'sadness': ['sadness', 'tears', 'loss', 'grief'], 'anger': ['anger', 'angry', 'fury', 'outrage'], 'pride': ['pride', 'hero', 'brave', 'courage'] } def count_emotion_words(text, categories): word_counts = {cat:0 for cat in categories} for word in text: for cat, keywords in categories.items(): if word in keywords: word_counts[cat] += 1 return word_counts

应用分析后,我们得到以下关键数据:

情感类型英文词频中文词频
悲伤2319
愤怒119
自豪86
希望57

5. 可视化与深度解读

将分析结果用Matplotlib呈现:

import matplotlib.pyplot as plt fig, ax = plt.subplots() emotions = ['sadness', 'anger', 'pride', 'hope'] counts = [23, 11, 8, 5] ax.bar(emotions, counts) ax.set_title('Emotion Distribution in Text') plt.show()

从数据中可以观察到几个有趣现象:

  1. 悲伤主导:与9/11事件背景相符,loss/grief类词汇高频出现
  2. 愤怒的复杂性:anger相关词汇多指向媒体和自身
  3. 英雄主义的双面性:pride与sacrifice词汇常相伴出现

注意:情感分析结果应与原文语境结合解读,单纯依赖统计数据可能导致误判

6. 技术应用的边界思考

在完成这个案例分析后,有几点技术反思值得分享:

  1. 语境的重要性:同一个词在不同段落可能表达完全相反的情感
  2. 文化差异:中英文版本的情感表达存在系统性差异
  3. 技术局限性:现有工具对反讽、隐喻等修辞手法识别有限
# 示例:同一词汇在不同语境下的情感差异 text1 = "she looked pretty much like the other widows" text2 = "she looked pretty in her new dress" print(analyzer.polarity_scores(text1)) # {'neg': 0.0, 'neu': 1.0, 'pos': 0.0} print(analyzer.polarity_scores(text2)) # {'neg': 0.0, 'neu': 0.0, 'pos': 1.0}

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询