基于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 的源码如下

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 爬取微博用户主页数据相关内容实现,大家有需要的可以相互学习一下。但是注意不可用于非法用途,远离"破坏计算机信息系统罪",慎重!慎重!慎重!

相关推荐
草履虫建模9 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
naruto_lnq11 小时前
分布式系统安全通信
开发语言·c++·算法
qq_2975746711 小时前
【实战教程】SpringBoot 实现多文件批量下载并打包为 ZIP 压缩包
java·spring boot·后端
老毛肚11 小时前
MyBatis插件原理及Spring集成
java·spring·mybatis
学嵌入式的小杨同学11 小时前
【Linux 封神之路】信号编程全解析:从信号基础到 MP3 播放器实战(含核心 API 与避坑指南)
java·linux·c语言·开发语言·vscode·vim·ux
lang2015092811 小时前
JSR-340 :高性能Web开发新标准
java·前端·servlet
Re.不晚11 小时前
Java入门17——异常
java·开发语言
缘空如是12 小时前
基础工具包之JSON 工厂类
java·json·json切换
精彩极了吧12 小时前
C语言基本语法-自定义类型:结构体&联合体&枚举
c语言·开发语言·枚举·结构体·内存对齐·位段·联合
追逐梦想的张小年12 小时前
JUC编程04
java·idea