admin
2019-08-26 d28bed1a1275131a5ca37f7da37961e2b518ac07
fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java
@@ -1,6 +1,9 @@
package com.yeshi.fanli.util.jd;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
@@ -8,6 +11,10 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
@@ -464,6 +471,7 @@
   /**
    * 根据链接提取商品ID
    *
    * @param url
    * @return
    */
@@ -481,4 +489,85 @@
      return null;
   }
   public static JDGoods getGoodsFromWeb(Long skuId) {
      String url = String.format("https://item.m.jd.com/product/%s.html", skuId + "");
      try {
         Document doc = Jsoup.connect(url).get();
         Elements scripts = doc.getElementsByTag("script");
         for (int i = 0; i < scripts.size(); i++) {
            String content = scripts.get(i).html();
            if (content.contains("window._itemOnly =")) {
               try {
                  String jsContent = "var windowData={};function getData(){return  JSON.stringify(windowData._itemOnly);}"
                        + "var _itemOnly={}; " + content.replace("window.", "windowData.");
                  System.out.println(jsContent);
                  ScriptEngineManager m = new ScriptEngineManager();
                  ScriptEngine engine = m.getEngineByName("JavaScript");
                  engine.eval(jsContent);
                  if (engine instanceof Invocable) {
                     Invocable in = (Invocable) engine;
                     String data = in.invokeFunction("getData").toString();
                     JSONObject root = JSONObject.fromObject(data);
                     root = root.optJSONObject("item");
                     JDGoods goods = new JDGoods();
                     goods.setSkuId(root.optLong("skuId"));
                     goods.setPicUrl("http://img14.360buyimg.com/n1/" + root.optJSONArray("image").optString(0));
                     goods.setSkuName(root.optString("skuName"));
                     return goods;
                  }
               } catch (Exception e) {
                  e.printStackTrace();
               }
            }
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
      return null;
   }
   public static String getJDGoodsJS() {
      InputStream input = JDUtil.class.getClassLoader().getResourceAsStream("jdGoods.js");
      StringBuilder sb = new StringBuilder();
      String line;
      BufferedReader br = new BufferedReader(new InputStreamReader(input));
      try {
         while ((line = br.readLine()) != null) {
            sb.append(line);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
      return sb.toString();
   }
   static String jdGoodsJs = null;
   static ScriptEngine engine = null;
   static {
      if (jdGoodsJs == null)
         jdGoodsJs = getJDGoodsJS();
      ScriptEngineManager manager = new ScriptEngineManager();
      engine = manager.getEngineByName("javascript");
      try {
         engine.eval(jdGoodsJs);
      } catch (Exception e) {
      }
   }
   public static String getJDGoodsId(String url) {
      try {
         if (engine instanceof Invocable) {
            Invocable in = (Invocable) engine;
            String goodsId = in.invokeFunction("getGoodsId", url).toString();
            if (!StringUtil.isNullOrEmpty(goodsId))
               return goodsId.trim();
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }
}