前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot批量爬取微信公众号信息及视频下载

springboot批量爬取微信公众号信息及视频下载

作者头像
程序员小藕
发布2021-08-10 10:05:30
1.1K1
发布2021-08-10 10:05:30
举报
文章被收录于专栏:程序员小藕程序员小藕

springboot批量爬取微信公众号信息及视频下载

1. 准备需要爬取的公众号链接(例如:https://mp.weixin.qq.com/s/GPz-w3_gS8jsgINJH9t6vw).下面的是整合了160多个公众号文章的地址.

2. 你们应该知道需要获取160个视频,先需要知道这160视频文章的地址.

    a. 搭建springboot框架.demo直通车.https://chenqiwei.lanzoui.com/isaWAschwji

    b.导入爬取网页的依赖在pom文件下.

代码语言:javascript
复制
<!-- 这个是爬取网页地址 -->

3.获取每个视频文章的地址

代码语言:javascript
复制
String url="https://mp.weixin.qq.com/s/GPz-w3_gS8jsgINJH9t6vw";
        Document document = Jsoup.parse(new URL(url), 30000);
        Elements section = document.getElementsByTag("a");
        StringBuffer stringBuffer = new StringBuffer();
        for (Element s:section){ //遍历每个视频文章集数
            VideoInfo videoInfo = new VideoInfo();
            String href = s.attr("href");//获取每个文章的地址
            String name = s.text();//获取每个文章的文本
}

4.每个的文章的视频,是需要再次请求微信服务器才会给你的 (获取的每个文章地址的mpvip的值替换下面这个地址的vid的地址才会返回真正的视频地址给你,就是文章mpcid替换途中红色部分再次请求,返回的才是视频地址)

代码语言:javascript
复制
https://mp.weixin.qq.com/mp/videoplayer?action=get_mp_video_play_url&preview=0&__biz=&mid=&idx=&vid=1070106698888740864&uin=&key=&pass_ticket=&wxtoken=&appmsg_token=&x5=0&f=json
代码语言:javascript
复制
if (href.contains("http")){
                Document parse = Jsoup.parse(new URL(href), 30000);
                String mpvid=parse.getElementsByTag("iframe").attr("data-mpvid");
                String sun="https://mp.weixin.qq.com/mp/videoplayer?action=get_mp_video_play_url&preview=0&__biz=&mid=&idx=&vid=1070106698888740864&uin=&key=&pass_ticket=&wxtoken=&appmsg_token=&x5=0&f=json";
                sun=sun.replace("1070106698888740864",mpvid);
                String s1 = sendGet(sun,"");
                String videourl=JSONObject.parseObject(s1).getJSONArray("url_info").getJSONObject(0).get("url").toString();
                String title=JSONObject.parseObject(s1).get("title").toString();
                String sss="<tr>\n" +
                        "        <td>"+name+"</td>\n" +
                        "        <td>"+title+"</td>\n" +
                        "        <td><a href='"+videourl.replace("http://","https://")+"'  target=\"_blank\">"+title+"视频地址点击下载</a></td>\n" +
                        "    </tr>";
                stringBuffer.append(sss);
            }

5.出来的效果.(每个视频地址就整理出来了)

6.完整的源码

代码语言:javascript
复制
 public static void main(String[] args) throws Exception {
        String url="https://mp.weixin.qq.com/s/GPz-w3_gS8jsgINJH9t6vw";
        Document document = Jsoup.parse(new URL(url), 30000);
        Elements section = document.getElementsByTag("a");
        StringBuffer stringBuffer = new StringBuffer();
        for (Element s:section){
            VideoInfo videoInfo = new VideoInfo();
            String href = s.attr("href");
            String name = s.text();
            if (href.contains("http")){
                Document parse = Jsoup.parse(new URL(href), 30000);
                String mpvid=parse.getElementsByTag("iframe").attr("data-mpvid");
                String sun="https://mp.weixin.qq.com/mp/videoplayer?action=get_mp_video_play_url&preview=0&__biz=&mid=&idx=&vid=1070106698888740864&uin=&key=&pass_ticket=&wxtoken=&appmsg_token=&x5=0&f=json";
                sun=sun.replace("1070106698888740864",mpvid);
                String s1 = sendGet(sun,"");
                String videourl=JSONObject.parseObject(s1).getJSONArray("url_info").getJSONObject(0).get("url").toString();
                String title=JSONObject.parseObject(s1).get("title").toString();
                String sss="<tr>\n" +
                        "        <td>"+name+"</td>\n" +
                        "        <td>"+title+"</td>\n" +
                        "        <td><a href='"+videourl.replace("http://","https://")+"'  target=\"_blank\">"+title+"视频地址点击下载</a></td>\n" +
                        "    </tr>";
                stringBuffer.append(sss);
            }
        }
        System.out.println(stringBuffer.toString());
    }

    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

7.关注博主公众号(内含更多技巧科技),一起学习更多知识.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-08-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • springboot批量爬取微信公众号信息及视频下载
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档