/** * @param url * @return */ public static ProductInfo parse(String url) { ProductInfo productInfo = new ProductInfo(); PrintLogTool.info("开始解析" + category.message() + "[URL=" + url + "]", logger); // 商品类型 productInfo.setCategory(category); try { doc = Jsoup.parse(new URL(url), 20000); } catch (MalformedURLException e) { logger.error("", e); } catch (IOException e) { logger.error("", e); } // 商品编号 setSerialNo(productInfo, url); // 当前价格 setCurrentPrice(productInfo); // 商品名称 setProductName(productInfo); // 商品图片 setProductPic(productInfo); return productInfo; }
private static void setCurrentPrice(ProductInfo productInfo) { Elements elements = doc.select("p.textPriceOur"); String currentPriceStr = elements.get(0).childNode(0).childNode(0).toString(); Money productCurrentPrice = new Money(currentPriceStr); productInfo.setProductCurrentPrice(productCurrentPrice); PrintLogTool.info(category.message() + "当前价格:[" + productCurrentPrice + "]", logger); }
// 内部方法 private static void setSerialNo(ProductInfo product, String url) { HttpServletRequest request = HttpRequestParser.parse(url); String uri = request.getRequestURI(); String[] pageArray = StringUtil.split(uri, DepreciateConstants.SEPARATOR_CHAR_SLASH); String page = pageArray[pageArray.length - 1]; String productSerialNo = StringUtil.split(page, DepreciateConstants.SEPARATOR_CHAR_PERIOD)[0]; product.setProductSerialNo(productSerialNo); PrintLogTool.info(category.message() + "商品编号:[" + productSerialNo + "]", logger); }
private static void setProductPic(ProductInfo productInfo) { List<ProductPicInfo> productPicInfos = new ArrayList<ProductPicInfo>(); Elements elements = doc.select("p.pBigPic > img"); for (Element _element : elements) { String url = _element.attr("src").toString(); ProductPicInfo productPicInfo = PicParse.parse(url); productPicInfos.add(productPicInfo); } productInfo.setProductPicInfos(productPicInfos); for (ProductPicInfo _productPicInfo : productPicInfos) { PrintLogTool.info( category.message() + "图片地址:[" + _productPicInfo.getPicSourceUrl() + "]", logger); } }
private static void setProductName(ProductInfo productInfo) { String productName = doc.title(); productInfo.setProductName(productName); PrintLogTool.info(category.message() + "商品名称:[" + productName + "]", logger); }