唐诗三百首数据采集、特征向量化与统计分析全流程代码实现
以下是根据您提供的要求完成的代码示例: 任务一:采集唐诗三百首的内容 ```python import requests from bs4 import BeautifulSoup # 定义函数用于请求网页并解析数据 def get_poems(): url = "https://so.gushiwen.cn/gushi/tangshi.aspx" response = requests.get(url) response.encoding = "utf-8" soup = BeautifulSoup(response.text, "html.parser") # 获取所有的诗列表 poem_list = soup.find_all("div", class_="typecont") poems = [] # 遍历诗列表,提取相关信息 for poem in poem_list: # 获取诗类型 poem_type = poem.find_previous_sibling("div", class_="bookMl").find("span").text # 获取诗题目 poem_title = poem.find("strong").text # 获取诗内容,去除换行符和制表符 poem_content = poem.find("span", style=False).text.replace("\n", "").replace("\t", "") # 获取诗作者 poem_author = poem.find_all("p")[1].find_all("a")[1].text # 将诗信息添加到列表中 poems.append([poem_type, poem_title, poem_content, poem_author]) return poems # 调用函数获取唐诗三百首的内容 poems_data = get_poems() # 将数据保存到txt文件 with open("poems.txt", "w", encoding="utf-8") as f: for poem in poems_data: f.write("\t".join(poem)) f.write("\n") ``` 任务二:文本特征向量化 ```python from sklearn.feature_extraction.text import TfidfVectorizer poem_contents = [poem[2] for poem in poems_data] # 创建tf-idf向量化对象 vectorizer = TfidfVectorizer() # 对诗内容进行向量化处理 X = vectorizer.fit_transform(poem_contents) # 获取特征词列表 feature_names = vectorizer.get_feature_names() # 将结果保存到txt文件 with open("tfidf_values.txt", "w", encoding="utf-8") as f: for i in range(len(poems_data)): tfidf_values = X[i].toarray().tolist()[0] poem_info = poems_data[i] poem_info.append(tfidf_values) f.write("\t".join(str(x) for x in poem_info)) f.write("\n") ``` 任务三:数据统计分析 ```python import pandas as pd import jieba from collections import Counter from wordcloud import WordCloud # 统计每种诗类型的数量 poem_types = [poem[0] for poem in poems_data] type_counts = Counter(poem_types) type_df = pd.DataFrame(list(type_counts.items()), columns=["诗类型", "数量"]) type_df.to_excel("poem_type_counts.xlsx", index=False) # 统计每个作者的数量 poem_authors = [poem[3] for poem in poems_data] author_counts = Counter(poem_authors) author_df = pd.DataFrame(list(author_counts.items()), columns=["作者", "数量"]) author_df.to_excel("poem_author_counts.xlsx", index=False) # 对诗内容进行中文分词 poem_contents = [poem[2] for poem in poems_data] text = " ".join(poem_contents) words = " ".join(jieba.cut(text)) # 生成词云图 wordcloud = WordCloud(font_path="simhei.ttf", width=800, height=600).generate(words) wordcloud.to_file("wordcloud.png") ``` 以上代码使用了requests库和BeautifulSoup库来请求和解析网页内容,使用了scikit-learn库的TfidfVectorizer类来进行文本特征向量化,使用了pandas库和jieba库进行数据统计分析,使用了wordcloud库生成词云图。部分步骤需要安装相应的库。 ######[AI写代码神器 | 2078点数解答 | 2024-06-23 11:15:17]
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)350
- Java代码实现从JSON数据中提取ID,涉及“数据”与“项目 - 知识库”结构(字节豆包 | 603点数解答 | 2025-04-15 22:48:51)244
- C++ 实现:计算一排鹅与最高、最矮鹅的身高差及代码详解(字节豆包 | 549点数解答 | 2025-11-16 20:19:06)88
- 编程揭秘:计算 𝑛! 在 𝑃 进制下末尾零个数的方法与代码实现(DeepSeek | 549点数解答 | 2026-01-11 17:49:54)51
- 工程师必知!Monkey 流程助力软件稳定性与可靠性提升(百度文心 | 388点数解答 | 2023-11-09 18:28:07)290
- 工程师揭秘:重要 APP 从需求到上线全流程攻略(百度文心 | 334点数解答 | 2023-11-09 18:31:47)289
- 打造智能任务调度与时间管理系统:功能全解析及实现秘籍(字节豆包 | 1346点数解答 | 2025-06-27 21:59:53)167
- 打造功能完备的任务调度与时间管理系统:从设计到实现全解析(DeepSeek | 708点数解答 | 2025-06-27 22:04:01)164
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)322
- 用 JS 中 for 循环实现 1 到 100 相加并输出结果到页面的完整代码 ( | 240点数解答 | 2024-05-20 22:11:29)475
- 用 JS 的 while 循环实现 1 到 100 相加并输出到页面的代码揭秘( | 47点数解答 | 2024-05-20 22:16:48)361
- Visual C++ 6.0:实现 n×n 矩阵最大最小元素所在行对调及输出(字节豆包 | 648点数解答 | 2024-11-02 10:23:57)312