python唐诗宋词经典案例
时间: 2023-06-25 15:06:25 浏览: 227
python经典案例
下面是一个使用Python爬虫和文本处理库jieba实现的唐诗宋词经典案例:
1. 爬取唐诗宋词文本数据
```python
import requests
# 爬取唐诗文本
def crawl_tang_poetry():
url = 'https://raw.githubusercontent.com/Werneror/Poetry/master/poetry_tang.txt'
response = requests.get(url)
text = response.text
return text
# 爬取宋词文本
def crawl_song_ci():
url = 'https://raw.githubusercontent.com/Werneror/Poetry/master/poetry_song.txt'
response = requests.get(url)
text = response.text
return text
```
2. 分词并统计词频
```python
import jieba
from collections import Counter
# 分词并统计词频
def word_count(text):
words = jieba.cut(text)
counter = Counter(words)
return counter
# 统计唐诗词频
tang_poetry = crawl_tang_poetry()
tang_counter = word_count(tang_poetry)
# 统计宋词词频
song_ci = crawl_song_ci()
song_counter = word_count(song_ci)
```
3. 生成词云图
```python
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 生成唐诗词云图
tang_wordcloud = WordCloud(width=800, height=600, background_color='white', font_path='msyh.ttc').generate_from_frequencies(tang_counter)
plt.imshow(tang_wordcloud)
plt.axis('off')
plt.show()
# 生成宋词词云图
song_wordcloud = WordCloud(width=800, height=600, background_color='white', font_path='msyh.ttc').generate_from_frequencies(song_counter)
plt.imshow(song_wordcloud)
plt.axis('off')
plt.show()
```
通过以上三步,我们就可以得到唐诗宋词的词云图了。这个案例可以帮助我们学习Python爬虫、文本处理和数据可视化的基础知识。
阅读全文