admin
2019-07-11 3824cbcaec6e6c67418d5280a53e9c2fedeef6f9
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
package com.yeshi.fanli.util.pinduoduo;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
 
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class PinDuoDuoUtil {
 
    public static List<String> getDetailImages(Long id) {
        List<String> imgList = new ArrayList<>();
        try {
            Document doc = Jsoup.connect("http://yangkeduo.com/goods.html?goods_id="+id)
                    .userAgent(
                            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36")
                    .get();
            Elements els = doc.getElementsByTag("script");
            for (int i = 0; i < els.size(); i++) {
                if (els.get(i).html().contains("window.rawData")) {
                    String dataJS = els.get(i).html().replace("window.", "var ");
                    dataJS += "function getData(){return JSON.stringify(rawData);}";
 
                    ScriptEngineManager manager = new ScriptEngineManager();
                    ScriptEngine engine = manager.getEngineByName("javascript");
                    try {
                        engine.eval(dataJS);
                        if (engine instanceof Invocable) {
                            Invocable in = (Invocable) engine;
                            String jsonStr = in.invokeFunction("getData").toString();
                            JSONObject json = JSONObject.fromObject(jsonStr);
                            JSONArray array = json.optJSONObject("store").optJSONObject("initDataObj")
                                    .optJSONObject("goods").optJSONArray("detailGallery");
                            for (int j = 0; j < array.size(); j++) {
                                imgList.add("http:" + array.optJSONObject(j).optString("url"));
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
 
        return imgList;
 
    }
 
}