| | |
| | | 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;
|
| | | import org.jsoup.Jsoup;
|
| | |
| | |
|
| | | /**
|
| | | * 根据链接提取商品ID
|
| | | * |
| | | * @param url
|
| | | * @return
|
| | | */
|
| | |
| | | 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 getJDGoodsId(String url) {
|
| | | try {
|
| | | if (url.startsWith("https://item.m.jd.com/product/") || url.startsWith("http://item.m.jd.com/product/")
|
| | | || url.startsWith("https://item.jd.com/") || url.startsWith("http://item.jd.com/")) {
|
| | | String preUrl = url.split("\\?")[0];
|
| | | String index = preUrl.split("/")[preUrl.split("/").length - 1];
|
| | | index = index.split("\\.")[0];
|
| | | return index.trim();
|
| | | } else if (url.startsWith("https://item.m.jd.com/ware/view.action?")) {
|
| | | String preUrl = url.substring(url.indexOf("?") + 1, url.length());
|
| | | String[] sts = preUrl.split("&");
|
| | | for (String st : sts) {
|
| | | if (st.startsWith("wareId=")) {
|
| | | return st.split("=")[1].trim();
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | } catch (Exception e) {
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | }
|