日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 爬关键词,Python爬虫实战:爬取微博热搜关键词

發布時間:2025/3/12 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 爬关键词,Python爬虫实战:爬取微博热搜关键词 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、爬取微博熱搜關鍵詞需要的第三方庫

1、requests

2、BeautifulSoup 美味湯

3、worldcloud 詞云

4、jieba 中文分詞

5、matplotlib 繪圖

二、爬取微博熱搜關鍵詞代碼示例import?requests

import?wordcloud

import?jieba

from?bs4?import?BeautifulSoup

from?matplotlib?import?pyplot?as?plt

from?pylab?import?mpl

#設置字體

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:

#獲取數據

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('微博熱搜關鍵詞詞云.png')

key?=?list(set(redata))

x,y?=?[],[]

#篩選數據

for?st?in?key:

count?=?redata.count(st)

if?count?<=?1:

continue

else:

x.append(st)

y.append(count)

x.sort()

y.sort()

#繪制結果圖

plt.plot(x,y)

plt.show()

except?Exception?as?e:

print(e)

以上就是Python爬蟲爬取微博熱搜關鍵詞的代碼示例,大家可以套入代碼直接使用哦~更多python爬蟲推薦:python爬蟲教程。

總結

以上是生活随笔為你收集整理的python 爬关键词,Python爬虫实战:爬取微博热搜关键词的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。