我又写了个好玩的工具

2022-05-30 09:09:44 浏览数 (1)

之前文章微博/公众号/抖音等各大平台都显示 ip 归属地了,能改吗? 我用代码统计了下公众号留言区的地区分布情况,广东的小伙伴最多:

今天再分析下微博评论区,以最近大火的刘耕宏这条微博为例https://m.weibo.cn/detail/4764632424907631

先抓取微博评论数据,包含评论时间,评论用户昵称,评论内容,点赞数和ip归属地等:

我打包了个exe软件 ,输入微博mid和微博cookie就行:

微博评论内容的词云图:

再用python统计IP归属地的占比图,来自广东和上海的最多:

代码语言:javascript复制
import requests,re,csv,time,random,pandas as pd
import numpy as np
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.charts import Pie
def ipData(mid):
    df = pd.read_csv(f"{mid}.csv",encoding='utf-8')#,on_bad_lines='skip'
    df2=df.ip归属地.value_counts().sort_values(ascending=False).head(10)
    ip = df2.index.tolist()
    counts = df2.values.tolist()
    bar = (
        Bar()
            .add_xaxis(ip)
            .add_yaxis("", counts)
    )
    pie = (
        Pie()
        .add("", [list(z) for z in zip(ip, counts)],radius=["40%", "75%"], )
        .set_global_opts(title_opts=opts.TitleOpts(title="饼图",pos_left="center",pos_top="20"))
        .set_global_opts(legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical"))
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"), )
    )
    pie.render(str(mid)  '.html')

另外微博转发数据的抓取可以用chrome扩展web scraper 不用写代码,Chrome 扩展神器 web scraper 抓取知乎热榜/话题/回答/专栏,豆瓣电影 ,还是以刘耕宏这条微博的转发为例 https://weibo.com/1767819164/Lr7nQkAHl?type=repost ,设置3个选择器:微博昵称,微博评论,评论时间。

看下抓取数据没问题。

最后导出excel文件:

为了方便大家学习数据抓取,我导出了sitemap,你可以直接导入使用。

代码语言:javascript复制
{"_id":"weibo","startUrl":["https://weibo.com/1767819164/Lr7nQkAHl?type=repost"],"selectors":[{"id":"content","type":"SelectorElementClick","parentSelectors":["_root"],"selector":"div.list_li","multiple":true,"delay":2000,"clickElementSelector":"a.page[action-data]","clickType":"clickOnce","discardInitialElements":"do-not-discard","clickElementUniquenessType":"uniqueText"},{"id":"微博昵称","type":"SelectorText","parentSelectors":["content"],"selector":".WB_text a[usercard]","multiple":false,"regex":"","delay":0},{"id":"微博评论","type":"SelectorText","parentSelectors":["content"],"selector":".WB_text span","multiple":false,"regex":"","delay":0},{"id":"评论时间","type":"SelectorText","parentSelectors":["content"],"selector":".WB_from a","multiple":false,"regex":"","delay":0}]}

如果还想分析微博正文看我之前文章一键批量下微博内容/图片/视频,获取博主最受欢迎微博,图片查找微博博主 ,比如李健微博每个月的转发评论赞数总和。

原创微博和转发比例。

发微博来源占比:

0 人点赞