Java版抖音解析接口

没有调用第三方接口,纯Java代码完成

使用IDEA编辑器直接打开Douyin文件夹,文件夹已打包上传到网盘

下载地址:https://www.lanzous.com/i4id9mb

Tools.java

package com.lhr;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Tools{
    private String cookies="odin_tt=9a16fa42e650a96379a5901a3d146c7c244dc0c35971927f6e13c208fc4bcf9cc952542516f78dc9098ac4d179f3b127cddfdff2942d259dda9ca33de8ae7677; install_id=43619087057; ttreq=1$4c4b4cc4b31e6f2f4203b62a1df12b43e224434c; qh[360]=1";

    public Tools(){

    }
    /**
     *
     * 这里获取作品ID
     * */
    public String getId (String url){
        String result=sendGet(url);
        result=getSubString(result,"/share/video/","/?");
        return result;
    }

    /**
     * 解析真实地址返回的数据其实是json格式的,Java语言本身不支持json解析,需要借助第三方jar
     *
     * 这里就直接使用getsubstring
     *
     * */
    public String getUrl (String url){
        String result=sendGet(url);
        result=getSubString(result,"play_addr_lowbr","width");
        result=getSubString(result,"url_list":["","","");
        return result;
    }

    /**
     * 取出中间文本
     *
     * */
    private String getSubString(String str,String left,String right){
        String strRight="";

        int indexLeft = str.indexOf(left);
        if(indexLeft==-1){
            return "";//没有找到直接返回空以免出现异常
        }else{
            strRight=str.substring(indexLeft);
        }
        int length=str.length()-strRight.length();
        int indexRight = strRight.indexOf(right);
        if (indexRight==-1){
            return "";
        }
        String result=str.substring(length+left.length(),length+indexRight);
        return result;
    }

    private String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        try {
            URL realUrl = new URL(url);
            //打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            //设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("Accept-Encoding", "utf-8");
            connection.setRequestProperty("Host", "api-hl.amemv.com");
            connection.setRequestProperty("user-agent","okhttp/3.10.0.1");
            connection.setRequestProperty("cookie",this.cookies);
            //建立实际的连接
            connection.connect();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        }catch(Exception e) {
            //发送异常
            return "发送失败,请检查URL地址是否正确";
        }finally{
            try{
                if(in != null){
                    in.close();
                }
            }catch(Exception e2) {
                //关闭异常
                System.out.println("关闭异常");
            }
        }
        return result;
    }
}

码云链接:

https://gitee.com/lhr0321/20175318_JAVA/tree/master/Douyin

原文地址:https://www.cnblogs.com/L1079991001/p/10995732.html