基于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);//获取数据 dataJSONObject data = jsonObject.getJSONObject("data");// 获取 User 信息JSONObject user = data.getJSONObject("user");String id = user.getString("id");//用户idString 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 爬取微博用户主页数据相关内容实现,大家有需要的可以相互学习一下。但是注意不可用于非法用途,远离“破坏计算机信息系统罪”,慎重!慎重!慎重!