-1

我正在尝试使用 JAVA、Jsoup 从 tikok 下载视频

到目前为止,我获取了 HTML 数据并解析了视频 json,我正在下载 JSON 格式的 downloadAddr,但它不起作用,而是返回 403 错误。如果有人有兴趣获取 Tiktok 视频,请看一下,以下是来自 tiktok url 的视频的 JSON 数据:https ://www.tiktok.com/@mamabeyou/video/6970003968055364870

"video": {
                    "id": "6854565358808993029",
                    "height": 1024,
                    "width": 576,
                    "duration": 15,
                    "ratio": "720p",
                    "cover": "https://p16-sign-va.tiktokcdn.com/obj/tos-maliva-p-0068/44a25ff93e034437b2d514760954d194?x-expires=1623074400&x-signature=2pSKwI5KwSHYJsXGOUCD8M%2BOoos%3D",
                    "originCover": "https://p16-sign-va.tiktokcdn.com/obj/tos-maliva-p-0068/6129799af243413a9a9833ffd5e43c84_1595952874?x-expires=1623074400&x-signature=wHSWcnE%2FP7NVPoavlVyA2sdtqac%3D",
                    "dynamicCover": "https://p16-sign-va.tiktokcdn.com/obj/tos-maliva-p-0068/8bf3a434271a4206b05cf6ba65e16827_1595952874?x-expires=1623074400&x-signature=Di%2Fv5s2xD%2Blkk%2B6jTST5fqWTrYc%3D",
                    "playAddr": "https://v16-web.tiktok.com/video/tos/useast2a/tos-useast2a-ve-0068c003/c1c3a0d1ef7848899a4bca12777bafc4/?a=1988&br=3754&bt=1877&cd=0%7C0%7C1&ch=0&cr=0&cs=0&cv=1&dr=0&ds=3&er=&expire=1623077258&l=202106070847230101902092304F04987A&lr=tiktok_m&mime_type=video_mp4&net=0&pl=0&policy=2&qs=0&rc=amo6Z2w8c3M3djMzZzczM0ApZzdkODdnOGRlNzxpZWU5aWc2b2lec2QxcWRfLS01MTZzcy8zLTRfXzQxL15hNmEvLjQ6Yw%3D%3D&signature=45e46160e31cd7a442fc5ec67671703f&tk=tt_webid_v2&vl=&vr=",
                    "downloadAddr": "https://v16-web.tiktok.com/video/tos/useast2a/tos-useast2a-ve-0068c003/c1c3a0d1ef7848899a4bca12777bafc4/?a=1988&br=3754&bt=1877&cd=0%7C0%7C1&ch=0&cr=0&cs=0&cv=1&dr=0&ds=3&er=&expire=1623077258&l=202106070847230101902092304F04987A&lr=tiktok_m&mime_type=video_mp4&net=0&pl=0&policy=2&qs=0&rc=amo6Z2w8c3M3djMzZzczM0ApZzdkODdnOGRlNzxpZWU5aWc2b2lec2QxcWRfLS01MTZzcy8zLTRfXzQxL15hNmEvLjQ6Yw%3D%3D&signature=45e46160e31cd7a442fc5ec67671703f&tk=tt_webid_v2&vl=&vr=",
                    "shareCover": [
                        "",
                        "https://p16-sign-va.tiktokcdn.com/tos-maliva-p-0068/6129799af243413a9a9833ffd5e43c84_1595952874~tplv-tiktok-play.jpeg?x-expires=1623074400&x-signature=c7L4j%2Bc0EV20sxNZyEdgSssX9y0%3D",
                        "https://p16-sign-va.tiktokcdn.com/tos-maliva-p-0068/6129799af243413a9a9833ffd5e43c84_1595952874~tplv-tiktok-play2.jpeg?x-expires=1623074400&x-signature=b8oIT3VesYUvNsgZiBScqApRZYM%3D"
                    ],
                    "reflowCover": "https://p16-sign-va.tiktokcdn.com/obj/tos-maliva-p-0068/44a25ff93e034437b2d514760954d194?x-expires=1623074400&x-signature=2pSKwI5KwSHYJsXGOUCD8M%2BOoos%3D",
                    "bitrate": 1922543,
                    "encodedType": "normal",
                    "format": "mp4",
                    "videoQuality": "normal",
                    "encodeUserTag": ""
                }

最后一步是根据 JSON 中的 downloadAddr 字段下载视频,我遇到的问题是 403 错误。下面是我的java代码,我的类中有一个main方法,你可以先检查:

package com.spider.utils;

import com.google.gson.Gson;
import com.spider.model.TikTokVideo;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class HTMLLoader {

    private static String cookiesStr = "tt_webid_v2=6968990058163848705; tt_webid=6968990058163848705; MONITOR_WEB_ID=6968990058163848705; _ga=GA1.2.1315239861.1622594624; passport_csrf_token=d08d697c6bf8996de200c21ef8be7c8f; passport_csrf_token_default=d08d697c6bf8996de200c21ef8be7c8f; passport_auth_status=4461f506dcbaaf2dc0ca07dbec6da47c%2C; passport_auth_status_ss=4461f506dcbaaf2dc0ca07dbec6da47c%2C; sid_guard=9eed4fa6763dceed51cd86d0ca27202e%7C1622769381%7C5184000%7CTue%2C+03-Aug-2021+01%3A16%3A21+GMT; uid_tt=1b45d3452d8c51a951b9159bffae68918d8fce847ff46c3b762385d076ad9b0d; uid_tt_ss=1b45d3452d8c51a951b9159bffae68918d8fce847ff46c3b762385d076ad9b0d; sid_tt=9eed4fa6763dceed51cd86d0ca27202e; sessionid=9eed4fa6763dceed51cd86d0ca27202e; sessionid_ss=9eed4fa6763dceed51cd86d0ca27202e; store-idc=alisg; store-country-code=cn; odin_tt=f9dfc6847fc4deb428d6560a0eee1ac6da7621e81e4bc66e48ca11d9b94e944ed465bc13dddaeecffe52066ab1f3c54de75bdb9d835ffab67ce46f6f38ec77f6209383977e1a0909c2043fabd8b45ff4; tt_csrf_token=dgOsuRkyci3vAyVbQrti_Ocf; csrf_session_id=a547a648622c44b9bca4ca0511e7540e; s_v_web_id=verify_kpiysnzz_xjFVty6E_o1pv_44hq_B1sf_4WKo9WU4hKnH; xgplayer_device_id=40509682259; xgplayer_user_id=703950030384; bm_sz=CBFF5398784179C9F43AAB6CBA6C039F~YAAQLHTCF/xyJst5AQAAC5P25AzUNANKT9ugqun9O+rFsGco3TICngDvsZZC2mV1sEGkchzYF38i9vYgaTSlxK41SIR17mjIQkw/fnhyeWJBMube6GkI8fv/BD7SzDOvtEXOerSCk8UwzBSaE2kxFC/K2j+Q/On9kOt1CGlkU2H7k0uzQMToSgvLNWEY0qTd; _abck=FAAE0713AB570A9C7357AC429302A0E8~-1~YAAQLHTCF/1yJst5AQAAC5P25AbsU8gINg7Sz+k5U1NJZK21u2/SfwcAiV1Z3rIbusd/Yn0Hao4t3rwmfr0cy/vQFoTPhb1/SJFjwbV2l5SESSp66mrYxnyseQF88EZyKLo0ZdLX+2a/X96ee0H3WVjTnc5bGUPaaTMIbj13s//1Na4GrbuOhjlDZzR3dGHILVw7uX6ies55m74WERBFLYLSPoEsBBl/1hT9Uq9MKpHCYDWkw54/vQ3N+Us9SglLZu5hXmZUlkl1hKCZ36CpyQrV8+vAnxMgNn3d6cjDqWK3vleTBb5vfx5DL602ByWHxOGAxjSdvLiU2f+Len+/iBILH/LEigNrW3xVPUldIjRG8Q/XqBZ+yMYMYPMBIRT2ssYaFG42YBjnlw==~-1~-1~-1; cmpl_token=AgQQAPPdF-RMpYxYfIog4Jk8-tdKu3OK_4PZYP9bfw; ttwid=1%7C_2DQhAb3FNdJO7TeEViwfBMb6tdOOpeS_jCEDDvWbQo%7C1623053481%7Cf9eca73a998726391f6ea768029a455df15d98514f9e2db9eaf76923815f1147; R6kq3TV7=AJW9huV5AQAAaPD3BP-74Sa5ENc8YjPGPmaoV6zprTPuHXNU0e8XxFEXLH7t|1|0|8b7c8d63a889aa2f48e4084853b1f4c3a58ad403; ak_bmsc=8878D47FB0062375E549B53BDBDC797617C2743D3E3B0000ADD4BD60ACBAC402~plxj3R+QWYAe5cIlRvJ6392wOlUtm3KxMZu769G1krI5pnVrzNig4P9mCPsOFc3hXyoagE5JOhfPSkkkjq/J+EjvZ2cSNwCovppDU4S7Klg4jSh+RwxUGMa7k48ab1HLSLcH2P3bSJpWyGQazN/Pen6+MtIS43hIndhCw9zTEELoJ0gEHoxlswYXfc1wGiyNOL2vR3YvQ+nEHuK2aXkwJzSriVYnH9yh8opa+39xVz7BQ=; bm_sv=16A7BCAC64C5999ECEFB83EAABF366C3~6vMhfeLgJoeI/bgNVuRnUopkAuGnrODmT0AvCYqnyXm1gcM0mckyo4UX1NRqb6yfoVgV0vqXbOs/kd3riX9Vv7zT1/2NxE5M2vZPq+1dKt9rULjESkhYSL558gY0AkstIaiPRAQSBQ+O5yonWRJ1Tbe2MH3fmgcfsp3VCiPAKYM=";

    private static String videoUrl = "https://www.tiktok.com/@mamabeyou/video/6970003968055364870?lang=en&is_copy_url=1&is_from_webapp=v1";

    //build cookie in req
    private static Map<String, String> buildCookiesMap(String cookieStr) {
        Map<String, String> cookieMap = new HashMap<String, String>();
        String[] cookieArr = cookieStr.split("; ");
        Arrays.stream(cookieArr).forEach(cookie -> {
            String[] split = cookie.split("=");
            cookieMap.put(split[0], split[1]);
        });
        return cookieMap;
    }

    //fetch HTML with Jsoup, and filter out video data in json.
    public static String loadHTMLByJsoup(String url) {

        Document doc = null;
        String jsCode = "";
        try {
            doc = Jsoup.connect("https://www.tiktok.com/@mamabeyou/video/6970003968055364870?lang=en&is_copy_url=1&is_from_webapp=v1")
                    .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36")
                    .cookies(buildCookiesMap(cookiesStr))
                    .timeout(100000)
                    .get();

        } catch (IOException e) {
            e.printStackTrace();
        }
        Element scriptElement = doc.selectFirst("script[id='__NEXT_DATA__']");
        //Elements newsHeadlines = doc.select("script[id='__NEXT_DATA__']");

        // Don't forget to check scriptElement is not null...
        if (scriptElement != null) {
            jsCode = scriptElement.html();
        } else {
            return "blank";
        }

        return jsCode;

    }

    //convert json to object
    public static TikTokVideo convertToObj(String jsonStr) {
        JSONObject jsonObject = new JSONObject(jsonStr).getJSONObject("props").getJSONObject("pageProps").getJSONObject("itemInfo").getJSONObject("itemStruct").getJSONObject("video");

        Gson gson = new Gson();
        TikTokVideo tkVideo = gson.fromJson(jsonObject.toString(), TikTokVideo.class);
        System.out.println(tkVideo);
        return tkVideo;
    }

    public static void downloadTikTokVid(String vidUrl) {
        FileOutputStream out = null;

        try {
            out = (new FileOutputStream(new File(File.separator + "tiktok.mp4")));
            out.write(Jsoup.connect(vidUrl)
                    .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36")
                    .cookies(buildCookiesMap(cookiesStr))
                    .ignoreContentType(true)
                    .execute()
                    .bodyAsBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) throws IOException {
        //fetch html data
        String jsonStr = loadHTMLByJsoup(videoUrl);
        //get video json data from HTML
        TikTokVideo tkVideo = convertToObj(jsonStr);
        //download video through video url
        downloadTikTokVid(tkVideo.getDownloadAddr());
        //get 403 error when download !!!

    }

}

4

2 回答 2

1

今天下午修好了。只需添加以下代码,然后它就可以工作了:

.referrer("https://www.tiktok.com/")
于 2021-06-07T15:02:23.297 回答
0

“downloadAddr”对你有用吗?似乎链接被破坏并且视频无法播放

于 2021-06-20T10:36:45.177 回答