利用Jsoup爬取天猫列表页数据

来源:互联网 发布:php curl get 数据 编辑:程序博客网 时间:2024/05/29 18:46

由于技术有限天猫详细页的销售数据爬取不到,所以采用折中的方法改为爬列表页.

本文针对的是店内搜索页


以下是获取网页数据:

/** * @param URL 根据URL获取document  */public static Document getDocument(String URL) {Connection conn = null;Document doc = null;int count = 0;while (doc == null && count < 3000) {try {conn = Jsoup.connect(URL);doc = conn.get();} catch (IOException e) {e.printStackTrace();}count++;}return doc;}

解析网页中的节点,获取数据

/** * @param prdListUrl *            :产品所在 列表页 * @param dataId *            :产品的数据ID * @throws Exception  */public static Map<String,String> getProductInfo(String prdListUrl,String dataId) throws Exception{Map<String,String> productInfo = new HashMap<String,String>();Document doc = GetTmall.getDocument(prdListUrl);Elements div_J_TItems = doc.select(".J_TItems");if(div_J_TItems!=null && !div_J_TItems.isEmpty()){Elements item5line1s = div_J_TItems.get(0).children(); for(Element item5line1 : item5line1s ){Elements items = item5line1.select(".item ");if(items != null && !items.isEmpty()){for(Element item : items){String prdId = item.attr("data-id");if(prdId.equals(dataId)){Elements c_prices = item.select(".detail .attribute .cprice-area .c-price");productInfo.put("price", c_prices.get(0).text());Elements sale_nums = item.select(".detail .attribute .sale-area .sale-num");productInfo.put("saleNum", sale_nums.get(0).text());}}}}}//System.out.println("商品网址:"+prdListUrl+"  商品编号 :"+dataId);//System.out.println("销售量:"+productInfo.get("saleNum")+"销售价:"+productInfo.get("price"));return productInfo;}


0 0
原创粉丝点击