唐诗三百首数据采集、特征向量化及统计分析全流程代码实现
任务一:从网站“https://so.gushiwen.cn/gushi/tangshi.aspx”中采集唐诗三百首的内容。 解析:首先需要使用Python的requests库获取网页内容,然后使用BeautifulSoup库解析网页,提取出古诗的类型、题目、内容和作者信息。最后将提取到的信息保存为txt格式的文本文件。 代码如下: ```python import requests from bs4 import BeautifulSoup url = "https://so.gushiwen.cn/gushi/tangshi.aspx" response = requests.get(url) response.encoding = 'utf-8' soup = BeautifulSoup(response.text, 'html.parser') poems = soup.find_all('div', class_='typecont') with open('poems.txt', 'w', encoding='utf-8') as f: for poem in poems: poem_type = poem.find('strong').text.strip() title = poem.find('h3').text.strip() content = poem.find('div', class_='contson').text.strip().replace(' ', '').replace('\t', '') author = poem.find('p', class_='source').find_all('a')[1].text.strip() f.write(f'{poem_type}\t{title}\t{content}\t{author} ') ``` 任务二:基于任务一的结果数据,针对“诗内容”,完成文本特征向量化(tf-idf值)输出。 解析:首先需要读取任务一生成的txt文件,然后使用jieba库进行中文分词,接着使用sklearn库的TfidfVectorizer类计算tf-idf值。最后将结果保存为txt格式的文本文件。 代码如下: ```python import jieba from sklearn.feature_extraction.text import TfidfVectorizer with open('poems.txt', 'r', encoding='utf-8') as f: lines = f.readlines() poems = [line.split('\t') for line in lines] contents = [' '.join(jieba.cut(poem[2])) for poem in poems] vectorizer = TfidfVectorizer() X = vectorizer.fit_transform(contents) with open('poems_tfidf.txt', 'w', encoding='utf-8') as f: for i, poem in enumerate(poems): tfidf_values = ' '.join([str(value) for value in X[i].toarray()[0]]) f.write(f'{poem[0]}\t{poem[1]}\t{poem[2]}\t{poem[3]}\t{tfidf_values} ') ``` 任务三:基于任务一的结果数据,完成诗词统计。 解析:首先需要读取任务一生成的txt文件,然后统计每种诗类型的诗数量和每个作者的诗数量。最后将结果保存为excel文件。对诗内容进行中文分词,并输出词云图。 代码如下: ```python import pandas as pd from wordcloud import WordCloud import matplotlib.pyplot as plt with open('poems.txt', 'r', encoding='utf-8') as f: lines = f.readlines() poems = [line.split('\t') for line in lines] # 统计每种诗类型的诗数量 poem_types = {} for poem in poems: if poem[0] not in poem_types: poem_types[poem[0]] = 0 poem_types[poem[0]] += 1 df_poem_types = pd.DataFrame(list(poem_types.items()), columns=['Poem Type', 'Count']) df_poem_types.to_excel('poem_types_count.xlsx', index=False) # 统计每个作者的诗数量 authors = {} for poem in poems: if poem[3] not in authors: authors[poem[3]] = 0 authors[poem[3]] += 1 df_authors = pd.DataFrame(list(authors.items()), columns=['Author', 'Count']) df_authors.to_excel('authors_count.xlsx', index=False) # 输出词云图 contents = [' '.join(jieba.cut(poem[2])) for poem in poems] words = ' '.join(contents) wordcloud = WordCloud(font_path='simhei.ttf', background_color='white').generate(words) plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.show() ``` ######[AI写代码神器 | 1159点数解答 | 2024-06-23 11:09:38]
- 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