| | |
| | |
|
| | | import org.apache.commons.httpclient.HttpClient;
|
| | | import org.apache.commons.httpclient.methods.PostMethod;
|
| | | import org.jsoup.Connection;
|
| | | import org.jsoup.Jsoup;
|
| | | import org.jsoup.nodes.Document;
|
| | | import org.jsoup.nodes.Element;
|
| | |
| | | import com.yeshi.fanli.dto.jd.JDSearchResult;
|
| | | import com.yeshi.fanli.dto.jd.JDShopInfo;
|
| | | import com.yeshi.fanli.entity.jd.JDGoods;
|
| | | import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
|
| | | import com.yeshi.fanli.tag.PageEntity;
|
| | | import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | |
| | |
|
| | | public class JDUtil {
|
| | |
|
| | | |
| | | public static final String TM_PHONE_URL = "https://item.m.jd.com/product/%s.html";
|
| | | |
| | | public void test() {
|
| | | Map<String, String> systemParams = new HashMap<>();
|
| | | systemParams.put("method", "");
|
| | |
| | | try {
|
| | | if (engine instanceof Invocable) {
|
| | | Invocable in = (Invocable) engine;
|
| | | String goodsId = in.invokeFunction("getGoodsId", url).toString();
|
| | | if (!StringUtil.isNullOrEmpty(goodsId))
|
| | | return goodsId.trim();
|
| | | Object goodsId = in.invokeFunction("getGoodsId", url);
|
| | | if (goodsId!=null)
|
| | | return goodsId.toString().trim();
|
| | | }
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 查询天猫商品图片、标题
|
| | | * @param auctionId
|
| | | * @return
|
| | | */
|
| | | public static JDGoods getSimpleGoodsInfo(String goodsId) {
|
| | | JDGoods jdGoods = null;
|
| | | try {
|
| | | Connection connect = Jsoup.connect(String.format(TM_PHONE_URL, goodsId));
|
| | | Document document = connect.get();
|
| | | Elements elements = document.getElementsByTag("script");
|
| | | if (elements.size() >= 0) {
|
| | | for (int i = 0; i < elements.size(); i++) {
|
| | | String content = elements.get(i).toString();
|
| | | if (content.contains("window._itemOnly")) {
|
| | | System.out.println("存在"); |
| | | content = content.replace("<script>", "");
|
| | | content = content.replace("</script>", "");
|
| | | int indexOf = content.indexOf("window._isLogin");
|
| | | content = content.substring(0,indexOf);
|
| | | |
| | | int indexItem = content.indexOf("\"item\":");
|
| | | content = content.substring(indexItem,content.length() - 1);
|
| | | content = content.replace("\"item\":", "var item =");
|
| | | content = content.replace("});", "").trim();
|
| | | |
| | | content+=";function getData() {return item;}";
|
| | | ScriptEngineManager manager = new ScriptEngineManager();
|
| | | ScriptEngine engine = manager.getEngineByName("javascript");
|
| | | engine.eval(content);
|
| | | |
| | | if (engine instanceof Invocable) {
|
| | | Invocable in = (Invocable) engine;
|
| | | JSONObject json = JSONObject.fromObject(in.invokeFunction("getData"));
|
| | | |
| | | jdGoods = new JDGoods();
|
| | | jdGoods.setSkuName(json.getString("skuName"));
|
| | | JSONObject jsonImage = json.getJSONObject("image");
|
| | | if (jsonImage != null && jsonImage.size() > 0) {
|
| | | String picUrl = jsonImage.getString("0");
|
| | | jdGoods.setPicUrl("http://m.360buyimg.com/mobilecms/s750x750_" + picUrl);
|
| | | }
|
| | | }
|
| | | |
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return jdGoods;
|
| | | }
|
| | |
|
| | | }
|