Java利用jsoup爬取抖音去水印视频--2020年08月最新有效代码

实现代码 

import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.*;

/**
 * 抖音去水印
 *
 * @author tarzan
 * @version 1.0
 * @date 2020/8/3
 * @since JDK1.8
 */
public class DouYin {

    //视频链接(固定)
    private static final String videoPath="https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=";

    //视频保存目录
    private static final String videoSavePath="d:/抖音视频/";

    //分享链接(手动修改)
    private static String targetPath = "#笑动欢乐秀 #沈腾 第一次“死”没有经验?天使:填个调查问卷吧 https://v.douyin.com/JjHGuMc/ 复制此链接,打开【抖音短视频】,直接观看视频!";



    public static void main(String[] args) throws IOException {
        Connection con= Jsoup.connect(filterUrl(targetPath));
        con.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1");
        Connection.Response resp=con.method(Connection.Method.GET).execute();
        String videoUrl= videoPath+getItemId(resp.url().toString());
        String jsonStr = Jsoup.connect(videoUrl).ignoreContentType(true).execute().body();
        JSONObject json =JSONObject.parseObject(jsonStr);
        String videoAddress= json.getJSONArray("item_list").getJSONObject(0).getJSONObject("video").getJSONObject("play_addr").getJSONArray("url_list").get(0).toString();
        String title= json.getJSONArray("item_list").getJSONObject(0).getJSONObject("share_info").getString("share_title");
        videoAddress=videoAddress.replaceAll("playwm","play");
   HashMap<String, String> headers = MapUtil.newHashMap();
            headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1");
            String finalVideoAddress = HttpUtil.createGet(videoAddress).addHeaders(headers).execute().header("Location");
           
        //注:打印获取的链接
        System.out.println("-----抖音去水印链接-----\n"+finalVideoAddress );
        //下载无水印视频到本地
        downVideo(finalVideoAddress,title);

    }

     /**
     * 方法描述: 下载无水印视频方法
     *
     * @param httpUrl
     * @param title
     * @author tarzan
     * @date 2020年08月04日 10:34:09
     */
    public static void downVideo(String httpUrl,String title) {
        String fileAddress = videoSavePath+title+".mp4";
        int byteRead;
        try {
            URL url = new URL(httpUrl);
            //获取链接
            URLConnection conn = url.openConnection();
            //输入流
            InputStream inStream = conn.getInputStream();
            //封装一个保存文件的路径对象
            File fileSavePath = new File(fileAddress);
            //注:如果保存文件夹不存在,那么则创建该文件夹
            File fileParent = fileSavePath.getParentFile();
            if(!fileParent.exists()){
                fileParent.mkdirs();
            }
            //写入文件
            FileOutputStream fs = new FileOutputStream(fileSavePath);
            byte[] buffer = new byte[1024];
            while ((byteRead = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteRead);
            }
            inStream.close();
            fs.close();
            System.out.println("\n-----视频保存路径-----\n"+fileSavePath.getAbsolutePath());
        } catch (FileNotFoundException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }

    /**
     * 方法描述: 获取分享视频id
     *
     * @param url
     * @Return {@link String}
     * @author tarzan
     * @date 2020年08月03日 17:36:12
     */
    public static String getItemId(String url){
        int start = url.indexOf("/video/")+7;
        int end = url.lastIndexOf("/");
        String itemId = url.substring(start, end);
        return  itemId;
    }

    /**
     * 方法描述: 过滤分享链接的中文汉字
     *
     * @param url
     * @Return {@link String}
     * @author tarzan
     * @date 2020年08月03日 17:36:33
     */
    public static String filterUrl(String url) {
        int start = url.indexOf("http");
        int end = url.lastIndexOf("/");
        String filterUrl = url.substring(start, end);
        return filterUrl;
    }
}

技术交流

微信:vxhqqh

猜你喜欢

转载自blog.csdn.net/weixin_40986713/article/details/107769979