百度热搜词词云图-python

本文介绍了一种使用Python生成百度各省份热搜词词云图的方法,通过爬虫抓取百度热搜词数据,利用WordCloud库结合中国地图背景生成可视化词云图,实现了热点词汇的直观展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

百度热搜词词云图-python

  Lan   2020-04-20 15:04   255 人阅读  0 条评论
import time
import imageio
import requests
from wordcloud import WordCloud

url = 'http://top.baidu.com/region/singlelist'

'''
省份热搜词
'''
# 城市列表
list = [928, 934, 911, 904, 909, 913, 925, 912, 902, 920, 921, 927, 908, 906, 930, 922, 916, 903, 907, 905, 919, 918,
        910, 914, 901, 929, 924, 923, 931, 932, 933, 926, 915, 917, ]
result = {}
# 循环获取信息
for i in list:
    data = {
        'boardid': '2',
        'divids[]': i,
    }
    html = requests.post(url=url, data=data).json()
    x = str(i)
    data = html['topWords'][x]
    lists = {i['keyword']: i['searches'] for i in data}
    result.update(lists)
# pic为背景形状
Pic = imageio.imread('chinamap.png')
# 实例化词云
w = WordCloud(width=1000, height=700, font_path='simhei.ttf', background_color='white', mask=Pic, scale=15)
# 将爬取的数据给词云
w.fit_words(result)
# 将结果根据时间写到图片
now = time.strftime('%Y-%m-%d', time.localtime())
w.to_file(f'{now}resou.png')


本文地址: https://www.lanol.cn/post/141.html
版权声明:本文为原创文章,版权归  Lan 所有,欢迎分享本文,转载请保留出处!
赞赏 打赏二维码

文章导航

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值