介绍
今天就跟大家聊聊有关使用Python怎么爬取微博热搜关键词,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
Python是什么意思
Python是一种跨平台的,具有解释性,编译性,互动性和面向对象的脚本语言,其最初的设计是用于编写自动化脚本,随着版本的不断更新和新功能的添加,常用于用于开发独立的项目和大型项目。
<强>一、爬取微博热搜关键词需要的第三方库强>
1,请求
2, BeautifulSoup美味汤
3, worldcloud词云
4, jieba中文分词
5, matplotlib绘图
<强>二,爬取微博热搜关键词代码示例强>
import 请求 import wordcloud import jieba 得到bs4 import  BeautifulSoup 得到matplotlib import  pyplot as  plt 得到pylab import  mpl #设置字体 mpl.rcParams [& # 39; font.sans-serif& # 39;],=, (& # 39; SimHei& # 39;】 mpl.rcParams [& # 39; axes.unicode_minus& # 39;],=,假的 时间=url & # 39; https://s.weibo.com/top/summary?Refer=top_hot& topnav=1, wvr=6 & # 39; 试一试: ,,,#获取数据 ,,,r =, requests.get (url) ,,,r.raise_for_status () ,,,r.encoding =r.apparent_encoding ,,,soup =, BeautifulSoup (r.text & # 39; html.parser& # 39;) ,,,data =, soup.find_all(& # 39;一个# 39;) ,,,d_list =, [] ,,,for item 拷贝数据: ,,,,,,,d_list.append (item.text) ,,,words =, d_list [4: -11:] ,,,#中文分词 ,,,result =,列表(jieba.cut(话说[0])) ,,,for word 拷贝文字[1::]: ,,,,,,,result.extend (jieba.cut(词)) ,,,redata =, [] ,,,for it 结果:拷贝 ,,,,,,,if len (it), & lt;=, 1: ,,,,,,,,,,,继续 ,,,,,,,其他的: ,,,,,,,,,,,redata.append (it) ,,,result_str =, & # 39; . join (redata), & # 39; ,,,#输出词云图 ,,,font =, " # 39; C: \ Windows \字体\ simhei.ttf& # 39; ,,,w =, wordcloud.WordCloud (font_path=字体宽度=600,身高=400) ,,,w.generate (result_str) ,,,w.to_file(& # 39;微博热搜关键词词云png # 39;) ,,,key =,列表(设置(redata)) ,,,x, y =, [], [] ,,,#筛选数据 ,,,for st 拷贝关键: ,,,,,,,count =, redata.count (st) ,,,,,,,if count & lt;=, 1: ,,,,,,,,,,,继续 ,,,,,,,其他的: ,,,,,,,,,,,x.append (st) ,,,,,,,,,,,y.append(计数) ,,,x.sort () ,,,y.sort () ,,,#绘制结果图 ,,,plt.plot (x, y) ,,,plt.show () except Exception  as e: ,,,印刷(e)