基于Java爬取微博数据(三) 微博主页用户数据

基于Java爬取微博数据三 微博主页用户数据

上一篇文章简单讲述了基于Java爬取微博数据(二),那么这篇将讲述如何基于 Java 爬取微博主页用户数据,下面开始具体的操作。

数据分析

在开始爬取微博主页用户数据之前,我们先对之前基于Java爬取微博数据(一)中的微博主页正文列表数据进行分析,看是否可以从中获取到微博主页用户数据。
首先还是按照基于Java爬取微博数据(一)中的方式获取微博主页正文列表数据内容
在这里插入图片描述
将获取到的数据取出一个微博内容的完整的 Json 对象,保存为 .json 文件
在这里插入图片描述
打开该微博正文内容,可以看到如下微博主页用户数据内容
在这里插入图片描述
但是这里看到,在实际的微博用户主页是还有用户的 粉丝数、关注数、主页描述、全部微博数等内容
在这里插入图片描述
一部分内容是无法从微博正文列表数据内容的 user 属性中获取,但是页面上可以展示,那么猜测这里应该是跳转到微博用户主页之后通过 ajax 异步加载了微博用户相关信息,那么继续查看 【网络】中相关请求,发现了一个获取 微博用户信息的 ajax 请求 /ajax/profile/info?uid=1686546714
在这里插入图片描述
取出请求 /ajax/profile/info?uid=1686546714 浏览器请求中的 响应 内容,可以看到我们需要的微博主页用户信息都有的
在这里插入图片描述
到这里,关于如何获取微博主页用户数据的数据分析就结束了,那么下面我们开始来写代码实现获取对应的微博主页用户数据。

爬取数据

这里我们重新创建一个 main 函数来单独的获取微博主页用户数据, DemoWeiBoInfo.java,整个类的代码比较简单,直接可以获取微博主页用户数据内容,最终执行的结果如图
在这里插入图片描述
DemoWeiBoInfo.java 的源码如下

package com.ruoyi.web.controller.demo.controller;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;

import java.text.ParseException;

public class DemoWeiBoInfo
{
    /**
     * 获取微博主页账号信息
     * @param args
     * @throws ParseException
     */
    public static void main(String[] args) throws ParseException {
        // 获取微博账号主页信息
        String url = "https://weibo.com/ajax/profile/info?uid=1686546714";
        String cookie = "你的 Cookie";

        System.out.println("微博账号信息查询开始");

        HttpResponse response = HttpUtil.createGet(url)
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36")
                .header("Cookie",cookie)
                .execute();
        String body = response.body();
        //System.out.println(body);
        if (StringUtils.isNotEmpty(body)) {
            JSONObject jsonObject = JSON.parseObject(body);
            //获取数据 data
            JSONObject data = jsonObject.getJSONObject("data");
            // 获取 User 信息
            JSONObject user = data.getJSONObject("user");
            String id = user.getString("id");
            //用户id
            String idstr = user.getString("idstr");
            System.out.println("idstr:" + idstr);
            //用户名
            String screen_name = user.getString("screen_name");
            System.out.println("screen_name:" + screen_name);
            JSONObject status_total_counter = user.getJSONObject("status_total_counter");
            // 转、评、赞 数量
            String total_cnt_format = status_total_counter.getString("total_cnt_format");
            System.out.println("total_cnt_format:" + total_cnt_format);
            String total_cnt = status_total_counter.getString("total_cnt");
            System.out.println("total_cnt:" + total_cnt);
            //评论数量
            String comment_cnt = status_total_counter.getString("comment_cnt");
            System.out.println("comment_cnt:" + comment_cnt);
            // 转发数量
            String repost_cnt = status_total_counter.getString("repost_cnt");
            System.out.println("repost_cnt:" + repost_cnt);
            // 获赞数量
            String like_cnt = status_total_counter.getString("like_cnt");
            System.out.println("like_cnt:" + like_cnt);
            //用户头像
            String avatar_large = user.getString("avatar_large");
            System.out.println("avatar_large:" + avatar_large);
            //描述
            String description = user.getString("description");
            System.out.println("description:" + description);
            // 粉丝数量
            String followers_count = user.getString("followers_count");
            System.out.println("followers_count:" + followers_count);
            String followers_count_str = user.getString("followers_count_str");
            System.out.println("followers_count_str:" + followers_count_str);
            // 关注数量
            String friends_count = user.getString("friends_count");
            System.out.println("friends_count:" + friends_count);
            //微博数量
            String statuses_count = user.getString("statuses_count");
            System.out.println("statuses_count:" + statuses_count);
        }
        System.out.println("微博账号信息查询结束");
    }
}

那么到这里,基于Java 爬取微博用户主页数据的任务就实现了,后续还会继续讲解获取微博正文内容图片、视频等相关内容,敬请关注。

注意点

这里需要说明的是,本文主要是探索基于 Java 爬取微博用户主页数据相关内容实现,大家有需要的可以相互学习一下。但是注意不可用于非法用途,远离“破坏计算机信息系统罪”,慎重!慎重!慎重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn565973850

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值