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

歡迎訪問 生活随笔!

生活随笔

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

python

Python定时爬取微博热搜+pyecharts动态图展示

發(fā)布時間:2023/12/10 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python定时爬取微博热搜+pyecharts动态图展示 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

作者:葉庭云

來源:凹凸數(shù)據(jù)

本文介紹了可以實現(xiàn)定時執(zhí)行任務的schedule模塊,利用它實現(xiàn)定時爬取微博熱搜數(shù)據(jù),保存到CSV文件里。

講解pyehcarts繪制基本時間輪播圖,最后利用pyehcarts實現(xiàn)數(shù)據(jù)的動態(tài)圖可視化。

微博熱搜

以下開始干貨實戰(zhàn)之旅 ?↓

schedule模塊定時執(zhí)行任務

python中有一個輕量級的定時任務調(diào)度的庫:schedule。他可以完成每分鐘,每小時,每天,周幾,特定日期的定時任務。因此十分方便我們執(zhí)行一些輕量級的定時任務。

#?安裝 pip?install?schedule?-i?http://pypi.douban.com/simple?--trusted-host?pypi.douban.com import?schedule import?timedef?run():print("I'm?doing?something...")schedule.every(10).minutes.do(run)????#?每隔十分鐘執(zhí)行一次任務 schedule.every().hour.do(run)?????????#?每隔一小時執(zhí)行一次任務 schedule.every().day.at("10:30").do(run)??#?每天的10:30執(zhí)行一次任務 schedule.every().monday.do(run)??#?每周一的這個時候執(zhí)行一次任務 schedule.every().wednesday.at("13:15").do(run)?#?每周三13:15執(zhí)行一次任務while?True:schedule.run_pending()??# run_pending:運行所有可以運行的任務

爬取微博熱搜數(shù)據(jù)

這樣的網(wǎng)頁結(jié)構(gòu)可以用 pd.read_html() 方法來爬取數(shù)據(jù)

#?-*-?coding:?UTF-8?-*- """ @File ???:微博熱搜榜.py @Author ?:葉庭云 @Date ???:2020/9/18 15:01 """ import?schedule import?pandas?as?pd from?datetime?import?datetime import?logginglogging.basicConfig(level=logging.INFO,?format='%(asctime)s?-?%(levelname)s:?%(message)s') count?=?0def?get_content():global?count???#?全局變量countprint('-----------?正在爬取數(shù)據(jù)?-------------')url?=?'https://s.weibo.com/top/summary?cate=realtimehot&sudaref=s.weibo.com&display=0&retcode=6102'df?=?pd.read_html(url)[0][1:11][['序號',?'關鍵詞']]???#?獲取熱搜前10time_?=?datetime.now().strftime("%Y/%m/%d?%H:%M")?????#?獲取當前時間df['序號']?=?df['序號'].apply(int)df['熱度']?=?df['關鍵詞'].str.split('??',?expand=True)[1]df['關鍵詞']?=?df['關鍵詞'].str.split('??',?expand=True)[0]df['時間']?=?[time_]?*?len(df['序號'])if?count?==?0:df.to_csv('datas.csv',?mode='a+',?index=False)count?+=?1else:df.to_csv('datas.csv',?mode='a+',?index=False,?header=False)#?定時爬蟲 schedule.every(1).minutes.do(get_content)while?True:schedule.run_pending()

微博熱搜一般是1分鐘更新一次,所以再給代碼加個定時器即可。讓程序跑一會兒,微博熱搜變動數(shù)據(jù)就保存到了CSV文件里。

pyehcarts動態(tài)圖可視化

基本時間輪播圖

from?pyecharts?import?options?as?opts from?pyecharts.charts?import?Bar,?Timeline from?pyecharts.faker?import?Faker from?pyecharts.globals?import?CurrentConfig,?ThemeTypeCurrentConfig.ONLINE_HOST?=?'D:/python/pyecharts-assets-master/assets/' tl?=?Timeline(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)) for?i?in?range(2015,?2020):bar?=?(Bar().add_xaxis(Faker.choose()).add_yaxis("商家A",?Faker.values()).add_yaxis("商家B",?Faker.values()).set_global_opts(title_opts=opts.TitleOpts("商店{}年商品銷售額".format(i))))tl.add(bar,?"{}年".format(i)) tl.render("timeline_multi_axis.html")

運行效果如下:

from?pyecharts?import?options?as?opts from?pyecharts.charts?import?Bar,?Timeline from?pyecharts.faker?import?Faker from?pyecharts.globals?import?ThemeType,?CurrentConfigCurrentConfig.ONLINE_HOST?=?'D:/python/pyecharts-assets-master/assets/' tl?=?Timeline(init_opts=opts.InitOpts(theme=ThemeType.DARK)) for?i?in?range(2015,?2020):bar?=?(Bar().add_xaxis(Faker.choose()).add_yaxis("商家A",?Faker.values(),?label_opts=opts.LabelOpts(position="right")).add_yaxis("商家B",?Faker.values(),?label_opts=opts.LabelOpts(position="right")).reversal_axis().set_global_opts(title_opts=opts.TitleOpts("Timeline-Bar-Reversal?(時間:?{}?年)".format(i))))tl.add(bar,?"{}年".format(i)) tl.render("timeline_bar_reversal.html")

運行效果如下:

微博熱搜動態(tài)圖

import?pandas?as?pd from?pyecharts?import?options?as?opts from?pyecharts.charts?import?Bar,?Timeline,?Grid from?pyecharts.globals?import?ThemeType,?CurrentConfigCurrentConfig.ONLINE_HOST?=?'D:/python/pyecharts-assets-master/assets/' df?=?pd.read_csv('datas.csv') #?print(df.info()) t?=?Timeline(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))??#?定制主題 for?i?in?range(34):bar?=?(Bar().add_xaxis(list(df['關鍵詞'][i*10:?i*10+10][::-1]))?????????#?x軸數(shù)據(jù).add_yaxis('熱度',?list(df['熱度'][i*10:?i*10+10][::-1]))???#?y軸數(shù)據(jù).reversal_axis()?????#?翻轉(zhuǎn).set_global_opts(????#?全局配置項title_opts=opts.TitleOpts(??#?標題配置項title=f"{list(df['時間'])[i*10]}",pos_right="5%",?pos_bottom="15%",title_textstyle_opts=opts.TextStyleOpts(font_family='KaiTi',?font_size=24,?color='#FF1493')),xaxis_opts=opts.AxisOpts(???#?x軸配置項splitline_opts=opts.SplitLineOpts(is_show=True),),yaxis_opts=opts.AxisOpts(???#?y軸配置項splitline_opts=opts.SplitLineOpts(is_show=True),axislabel_opts=opts.LabelOpts(color='#DC143C'))).set_series_opts(????#?系列配置項label_opts=opts.LabelOpts(??#?標簽配置position="right",?color='#9400D3')))grid?=?(Grid().add(bar,?grid_opts=opts.GridOpts(pos_left="24%")))t.add(grid,?"")t.add_schema(play_interval=100,??????????#?輪播速度is_timeline_show=False,?????#?是否顯示?timeline?組件is_auto_play=True,??????????#?是否自動播放)t.render('時間輪播圖.html')

運行結(jié)果如下:

↑ 演示圖

如果大家對我分享的 Python定時爬蟲-爬取微博熱搜數(shù)據(jù) -pyecharts動態(tài)圖展示 感興趣,歡迎轉(zhuǎn)發(fā)、在看、點贊三連!

●Python數(shù)據(jù)可視化教程實戰(zhàn)!

●取數(shù),取數(shù),取個屁啊!

后臺回復“入群”即可加入小z數(shù)據(jù)干貨交流群

總結(jié)

以上是生活随笔為你收集整理的Python定时爬取微博热搜+pyecharts动态图展示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。