python唐诗分析综合_Python爬虫抓取唐诗宋词

一 说明

Python语言的爬虫开发相对于其他编程语言是极其高校的,在上一篇文章 爬虫抓取博客园前10页标题带有Python关键字(不区分大小写) 的文章中,我们介绍了使用requests做爬虫开发,它能处理简单 的任务,也是入门爬虫最简单的方式。接下来我们将为大家介绍使用 beautiful soup 库 来做稍微复杂一点的任务。

二 实操

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @Time : 2020/7/23 5:58 下午

# @Author : Albert Ma

# @File : test1.py

import requests

from bs4 import BeautifulSoup

##################

# 唐诗300首代码开始

##################

numbers = []

dynasties = []

poets = []

names = []

poems = []

for i in range(1, 17):

i = str(i)

url = 'http://www.shicimingju.com/shicimark/tangshisanbaishou_' + i + '_0__0.html'

r = requests.get(url)

demo = r.text # 服务器返回响应

soup = BeautifulSoup(demo, "html.parser&

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用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爬虫、文本处理和数据可视化的基础知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值