您现在的位置是:貌合心离网 > 热点
手把手教你对文本文件进行分词、词频统计和可视化(附源码)
貌合心离网2026-11-14【热点】{固定数字1}人已围观
简介谷歌快排【tg:seogoogle987】
大家好!我是Python进阶者。
前言
前几天一个在校大学生问了一些关于词频、分词和可视化方面的问题,结合爬虫,确实可以做点东西出来,可以玩玩,还是蛮不错的,这里整理成一篇文章,分享给大家。
本文主要涉及的库有爬虫库requests、词频统计库collections、数据处理库numpy、结巴分词库jieba 、可视化库pyecharts等等。
一、数据来源
关于数据方面,这里直接是从新闻平台上进行获取的文本信息,其实这个文本文件可以拓展开来,你可以自定义文本,也可以是报告,商业报告,政治报告等,也可以是新闻平台,也可以是论文,也可以是微博热评,也可以是网易云音乐热评等等,只要涉及到大量文本的手把,都可月引用本文的代码,进行词频分词、统计、可视化等。
二、数据获取
数据获取十分简单,一个简单的爬虫和存储就可以搞定,这里以一篇新闻为例进行演示,代码如下:
import re import collections # 词频统计库 import numpy as np # numpy数据处理库 import jieba # 结巴分词 import requests from bs4&nbs手教p;import BeautifulSoup from pyecharts import options as opts from pyecharts.charts import WordCloud from pyecharts.globals import SymbolType import warnings warnings.filterwarnings('ignore')  你对;r=requests.get("https://m.thepaper.cn/baijiahao_11694997",timeout=10) r.encoding="utf-8" s=BeautifulSoup(r.text,"html.parser") f=open("报告.txt","w",encoding="utf-8") L=s.find_all("p") for c in L: f.write("{} ".format(c.text))&n文本bsp; f.close()
代码运行之后,在本地会得到一个【报告.txt】文件,文件内容就是网站上的文本信息。如果你想获取其他网站上的文本,需要更改下链接和提取规则。
三、词频统计
接下来就是词频统计了,代码如下所示。
# 读取文件 fn = open("./报告.txt","r",encoding="utf-8") string_data = fn.read() fn.close() # 文本预处理 # 定义正则表达式匹文件配模式 pattern = re.compile(u' |,|/|。| |.|-|:|;|)|(|?|"') string_data = re.sub(pattern,'',string_data) # 将符合模式的字符去除 # 文本分词 # 精确模式分词 seg_list_exact = jieba.cut(string_data,cut_all=False) object_list =进行 [] # 自定义去除词库 remove_words = [u'的',u'Ҫ', u'“',u'”',u'和',u',',u'Ϊ',u'是', '以' u'随着', u'对于', u'对',u'等',u'能',u'都',u'。', &n分词bsp; u' ',u'、',u'中',u'在',u'了',u'通常',u'如果',u'我', u'她',u'(',u')',u'他',u'你',u'?',u'—',u'就', &nbs、词p; u'着',u'˵',u'上',u'这', u'那',u'有', u'Ҳ', u'ʲô', u'·', u'将', u'没有', u'到', u'不', u'ȥ'] for word频统 in seg_list_exact: if word not in remove_words: object_list.append(word) # 词频统计 # 对分词做词频统计 word_counts = collections.Counter(object_list) # 计和;获取前30最高频的词 word_counts_all = word_counts.most_common() word_counts_top30 = word_counts.most_common(30) print("2021年政府工作报告一共有%d个词"%len(word_counts)) print(word_counts_top30)
首先读取文本信息,之后对文本进行预处理,提取文字信息,并且可以自定义词库,作为停用词,之后将获取到的词频做词频统计,获取前30最高频的词,并进行打印,输出结果如可视下图所示。
四、可视化
接下来就是可视化部分了,这里直接上代码,如下所示。
import pyecharts from pyecharts.charts import Line from pyecharts import options as opts # 示例数据 cate = [i[0] for i in word_counts_top30] data1 = [化(i[1] for i in word_counts_top30] line = (Line() .add_xaxis(cate) .add_yaxis('词频', data1, &n附源bsp; markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")])) .set_global_opts(title_opts=opts.TitleOpts(title="词频统计Top30", subtitle=""), xax码)is_opts=opts.AxisOpts(name_rotate=60,axislabel_opts={"rotate":45})) ) line.render_notebook()
输出结果是一个线图,看上去还不错。
五、总结
本文基于Python网络爬虫获取到的文本文件,通过词频、分词和可视化等处理,完成一个较为简单的项目,欢迎大家积极尝试。在代码实现过程中,如果有遇到任何问题,请加我好友,我帮助解决哦!
很赞哦!(020)
站长推荐
友情链接
- 标签: 康师傅
- https://www.whatsonstage.com/search?cat=64&term=谷歌快排【tg:seogoogle987】,7Y2
- https://www.maigoo.com/search/?block=&q=谷歌快排【tg:seogoogle987】,uQp
- https://www.military-history.org/?s=谷歌快排【tg:seogoogle987】,5rU
- https://www.sic-info.org/?s=谷歌快排【tg:seogoogle987】,M9l
- https://www.shacknews.com/editorial-search?q=谷歌快排【tg:seogoogle987】,dqJ
- https://www.paspaley.com/catalogsearch/result/?action=QUERY&q=谷歌快排【tg:seogoogle987】,KY5
- https://www.springfieldlibrary.org/?s=谷歌快排【tg:seogoogle987】,p8i
- https://www.impactpool.org/search/?type=79&q=谷歌快排【tg:seogoogle987】Eqw
- https://www.spoiledsweetboutique.com/search/谷歌快排【tg:seogoogle987】,OI4
- https://www.rookwinkel.nl/catalogsearch/result/index/?q=谷歌快排【tg:seogoogle987】,x9D
- https://www.southmeadows.co.uk/2019/07/?s=谷歌快排【tg:seogoogle987】,vgL
- https://m.shoppinghow.kakao.com/m/search/q/谷歌快排【tg:seogoogle987】NyS
- https://www.pickett.co.uk/collections/vendors?q=谷歌快排【tg:seogoogle987】,SqN
- https://www.teamlab.art/es/?submit=谷歌快排【tg:seogoogle987】,qHQ
- https://www.madebyoranges.com/nl/search/谷歌快排【tg:seogoogle987】,SFv
- https://www.otakuninjahero.com/en/search/谷歌快排【tg:seogoogle987】,Vvg
- https://www.metalmensales.com/?action=QUERY&s=谷歌快排【tg:seogoogle987】,ZAj
- https://www.lee.com/search?q=谷歌快排【tg:seogoogle987】,giW
- https://www.yli.ro/catalogsearch/result/?q=谷歌快排【tg:seogoogle987】,pqI
- https://www.pearson.com/en-us/search.html?aq=谷歌快排【tg:seogoogle987】,rJa
- https://www.openspaceinstitute.org/search?q=谷歌快排【tg:seogoogle987】,ee7
- https://www.schenckprocess.com/search?q=谷歌快排【tg:seogoogle987】,u5F
- https://www.udc.co.uk/index.php?route=product/search&sort=p.price&order=DESC&tag=谷歌快排【tg:seogoogle987】,wGG
- https://www.repetto.jp/item/search?action=QUERY&q=谷歌快排【tg:seogoogle987】,aAv
- https://www.pearlstonecenter.org/search/谷歌快排【tg:seogoogle987】,BGX
- https://www.reallywildbirdfood.co.uk/store/search?q=谷歌快排【tg:seogoogle987】,mSl
- https://www.teamlab.art/ko/?submit=谷歌快排【tg:seogoogle987】,zP2
- https://www.kubb-tool.bfs.admin.ch/en/search?action=CHECK&q=谷歌快排【tg:seogoogle987】,f1A
- https://www.oikos-tecnics.com/?s=谷歌快排【tg:seogoogle987】,X7r
- https://www.zutari.com/?s=谷歌快排【tg:seogoogle987】,co3
- https://www.mexperience.com/?T=1686132112324&s=谷歌快排【tg:seogoogle987】,lFN
- https://www.kyuhoshi.com/?s=谷歌快排【tg:seogoogle987】,Rmv
- https://www.xbiao.com/search/index?tpf&wd=谷歌快排【tg:seogoogle987】,dGd
- https://www.nuovabai.it/catalogsearch/result/?action=SEARCH&q=谷歌快排【tg:seogoogle987】,lpJ
- https://www.rockngem.com/?action=CHECK&s=谷歌快排【tg:seogoogle987】,NSG
- https://www.tombow.com/search/谷歌快排【tg:seogoogle987】,rwx
- https://www.nextavenue.org/?action=QUERY&s=谷歌快排【tg:seogoogle987】,5sA
- https://www.tycoonstory.com/?s=谷歌快排【tg:seogoogle987】,goS
- https://www.trockenmittel.ch/catalogsearch/result/?iC=ZM3&q=谷歌快排【tg:seogoogle987】,LXl
- https://jihoon471.cafe24.com/product/search.html?banner_action=&keyword=谷歌快排【tg:seogoogle987】u5p
- https://www.olx.ba/pretraga?trazilica=谷歌快排【tg:seogoogle987】,m5i
- https://www.ng-slo.si/si/iskanje?cat=114&q=谷歌快排【tg:seogoogle987】,YJz
- https://nicolazzi.it/en/?s=谷歌快排【tg:seogoogle987】,v9s
- https://www.pinkpanda.ro/magazin/ingrijirea-tenului?page=87&q=谷歌快排【tg:seogoogle987】,h21
- https://www.qwant.com/?q=谷歌快排【tg:seogoogle987】,Kh0
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=谷歌快排【tg:seogoogle987】LJy
- https://www.spicebar.de/catalogsearch/result/?q=谷歌快排【tg:seogoogle987】,TmI
- https://hsmoa.com/search?query=谷歌快排【tg:seogoogle987】qz2
- https://www.uctronics.com/catalogsearch/result/?q=谷歌快排【tg:seogoogle987】,6WJ
- https://www.krisinformation.se/en/search?q=谷歌快排【tg:seogoogle987】,d9F







