ITPub博客

首页 > IT职业 > IT图书 > Python爬虫实战:爬取微博热搜关键词

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

IT图书 作者:zuike2000 时间:2021-09-11 09:59:50 0 删除 编辑

一、爬取微博热搜关键词需要的第三方库

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:WindowsFontssimhei.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爬虫爬取微博热搜关键词的代码示例,大家可以套入代码直接使用哦~如果大家想测试使用下,可以尝试品易云http代理ip,免费测试提供1000个国内外ip地址,希望对大家有所帮助!

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2203/viewspace-2830711/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    404
  • 访问量
    2063