admin
2021-09-24 f788607ff771a47bc60d6a86e00b3433c40f3d2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.yeshi.buwan.util.video.web;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
 
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.lang.reflect.Type;
import java.util.List;
 
public class YouKuWebUtil {
 
 
    public static void main(String[] args) throws Exception {
 
        List<YouKuCoverInfo> list = parseCategoryList("https://www.youku.com/category/show/c_97_s_6.html?theme=dark");
        System.out.println(list);
    }
 
 
    /**
     * 解析专辑列表
     *
     * @param url
     */
    public static List<YouKuCoverInfo> parseCategoryList(String url) throws Exception {
 
        Document document = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36").timeout(60000).get();
        Elements els = document.getElementsByTag("script");
        for (int i = 0; i < els.size(); i++) {
            String value = els.get(i).html();
            if (value.contains("window.__INITIAL_DATA__")) {
                System.out.println(value);
                return parseVideoList(value);
            }
        }
        return null;
    }
 
 
    private static List<YouKuCoverInfo> parseVideoList(String dtaa) throws ScriptException, NoSuchMethodException {
        String script = "var _window={};" + dtaa.replace("window.", "_window.").replace("document.", "_document.");
        script += ";function getData(){return  JSON.stringify(_window.__INITIAL_DATA__.categoryVideos);}";
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine jsEngine = manager.getEngineByName("javascript");
        try {
            jsEngine.eval(script);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
 
        Gson gson = new Gson();
        if (jsEngine instanceof Invocable) {
            Invocable in = (Invocable) jsEngine;
            Object data = in.invokeFunction("getData");
            Type type = new TypeToken<List<YouKuCoverInfo>>() {
            }.getType();
            List<YouKuCoverInfo> list = gson.fromJson(data.toString(), type);
            if (list != null) {
                for (YouKuCoverInfo info : list) {
                    info.setShowId(parseShowIdFromVideoLink(info.getVideoLink()));
                }
            }
            return list;
        }
        return null;
    }
 
    private static String parseShowIdFromVideoLink(String videoLink) {
        if (videoLink.indexOf("/id_") > -1) {
            return videoLink.substring(videoLink.indexOf("/id_"), videoLink.indexOf(".htm")).replace("/id_", "").trim();
        }
 
        return null;
 
    }
 
    public static class YouKuCoverInfo {
        private String summaryType;
        private String access;
        private String type;
        private String showThumb;
        private String img;
        private String summary;
        private String title;
        private String subTitle;
        private String rightTagText;
        private String videoId;
        private String videoLink;
 
        private String showId;
 
        public String getShowId() {
            return showId;
        }
 
        public void setShowId(String showId) {
            this.showId = showId;
        }
 
        public String getSummaryType() {
            return summaryType;
        }
 
        public void setSummaryType(String summaryType) {
            this.summaryType = summaryType;
        }
 
        public String getAccess() {
            return access;
        }
 
        public void setAccess(String access) {
            this.access = access;
        }
 
        public String getType() {
            return type;
        }
 
        public void setType(String type) {
            this.type = type;
        }
 
        public String getShowThumb() {
            return showThumb;
        }
 
        public void setShowThumb(String showThumb) {
            this.showThumb = showThumb;
        }
 
        public String getImg() {
            return img;
        }
 
        public void setImg(String img) {
            this.img = img;
        }
 
        public String getSummary() {
            return summary;
        }
 
        public void setSummary(String summary) {
            this.summary = summary;
        }
 
        public String getTitle() {
            return title;
        }
 
        public void setTitle(String title) {
            this.title = title;
        }
 
        public String getSubTitle() {
            return subTitle;
        }
 
        public void setSubTitle(String subTitle) {
            this.subTitle = subTitle;
        }
 
        public String getRightTagText() {
            return rightTagText;
        }
 
        public void setRightTagText(String rightTagText) {
            this.rightTagText = rightTagText;
        }
 
        public String getVideoId() {
            return videoId;
        }
 
        public void setVideoId(String videoId) {
            this.videoId = videoId;
        }
 
        public String getVideoLink() {
            return videoLink;
        }
 
        public void setVideoLink(String videoLink) {
            this.videoLink = videoLink;
        }
    }
 
 
}