Python爬蟲分析微博熱搜關(guān)鍵詞的實(shí)現(xiàn)代碼
1,使用到的第三方庫
requests
BeautifulSoup 美味湯
worldcloud 詞云
jieba 中文分詞
matplotlib 繪圖
2,代碼實(shí)現(xiàn)部分
import requests
import wordcloud
import jieba
from bs4 import BeautifulSoup
from matplotlib import pyplot as plt
from pylab import mpl
#設(shè)置字體
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
url = 'https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6'
try:
#獲取數(shù)據(jù)
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
soup = BeautifulSoup(r.text,'html.parser')
data = soup.find_all('a')
d_list = []
for item in data:
d_list.append(item.text)
words = d_list[4:-11:]
#中文分詞
result = list(jieba.cut(words[0]))
for word in words[1::]:
result.extend(jieba.cut(word))
redata = []
for it in result:
if len(it) <= 1:
continue
else:
redata.append(it)
result_str = ' '.join(redata)
#輸出詞云圖
font = r'C:\Windows\Fonts\simhei.ttf'
w = wordcloud.WordCloud(font_path=font,width=600,height=400)
w.generate(result_str)
w.to_file('微博熱搜關(guān)鍵詞詞云.png')
key = list(set(redata))
x,y = [],[]
#篩選數(shù)據(jù)
for st in key:
count = redata.count(st)
if count <= 1:
continue
else:
x.append(st)
y.append(count)
x.sort()
y.sort()
#繪制結(jié)果圖
plt.plot(x,y)
plt.show()
except Exception as e:
print(e)
3,運(yùn)行結(jié)果


到此這篇關(guān)于Python爬蟲分析微博熱搜關(guān)鍵詞的文章就介紹到這了,更多相關(guān)Python爬蟲微博熱搜內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pandas進(jìn)行時(shí)間數(shù)據(jù)的轉(zhuǎn)換和計(jì)算時(shí)間差并提取年月日
這篇文章主要介紹了pandas進(jìn)行時(shí)間數(shù)據(jù)的轉(zhuǎn)換和計(jì)算時(shí)間差并提取年月日,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python3利用Dlib實(shí)現(xiàn)攝像頭實(shí)時(shí)人臉檢測(cè)和平鋪顯示示例
這篇文章主要介紹了Python3利用Dlib實(shí)現(xiàn)攝像頭實(shí)時(shí)人臉檢測(cè)和平鋪顯示示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
對(duì)Python 內(nèi)建函數(shù)和保留字詳解
今天小編就為大家分享一篇對(duì)Python 內(nèi)建函數(shù)和保留字詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
python+tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)單的秒鐘
這篇文章主要為大家詳細(xì)介紹了Python如何利用tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)單的秒鐘,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以自己動(dòng)手嘗試一下2024-02-02
python中使用requests設(shè)置代理服務(wù)器
文章介紹了代理服務(wù)器的工作原理和使用方法,包括代理的概念、代理服務(wù)器的作用、如何在Python中設(shè)置代理以及代理的匿名度分類2024-11-11

