是不是所有練習爬蟲的人都會去爬取商品信息。。。
下面是爬取京東上商品信息的代碼(只爬取了一頁數據)
public void downJDProduct() throws IOException { String input = "辣條";// 以辣條為例,可以給這個方法加一個參數,這樣就能接收用戶輸入進行爬取 // 需要爬取商品信息的網站地址 String url = "https://search.jd.com/Search?keyword=" + input + "&enc=utf-8"; // 動態模擬請求數據 CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); // 模擬瀏覽器瀏覽(user-agent的值可以通過瀏覽器瀏覽,查看發出請求的頭文件獲取) httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"); CloseableHttpResponse response = httpclient.execute(httpGet); // 獲取響應狀態碼 int statusCode = response.getStatusLine().getStatusCode(); try { HttpEntity entity = response.getEntity(); // 如果狀態響應碼為200,則獲取html實體內容或者json文件 if (statusCode == 200) { String html = EntityUtils.toString(entity, Consts.UTF_8); // 提取HTML得到商品信息結果 Document doc = Jsoup.parse(html); // 通過瀏覽器查看商品頁面的源代碼,找到信息所在的div標簽,再對其進行一步一步地解析,這都需要對html代碼進行分析了 Elements ulList = doc.select("#J_goodsList"); Elements liList = ulList.select(".gl-item"); // 循環liList的數據(具體獲取的數據值還得看doc的頁面源代碼來獲取,可能稍有變動) for (Element item : liList) { // 商品ID String id = item.attr("data-sku"); System.out.println("商品ID:" + id); // 商品名稱 String name = item.select(".p-name").select("em").text(); System.out.println("商品名稱:" + name); // 商品價格 String price = item.select(".p-price").select("i").text(); System.out.println("商品價格:" + price); // 商品網址 String goodsUrl = item.select(".p-name").select("a").attr("href"); System.out.println("商品網址:" + goodsUrl); // 商品圖片網址 String imgUrl = item.select(".p-img").select("a").select("img").attr("src"); System.out.println("商品圖片網址:" + imgUrl); // 商品店鋪 String goodsShop = item.select(".p-shop").select("span").select("a").attr("title"); System.out.println("商品店鋪名稱:" + goodsShop); System.out.println("------------------------------------"); } // 消耗掉實體 EntityUtils.consume(response.getEntity()); } else { // 消耗掉實體 EntityUtils.consume(response.getEntity()); } } finally { response.close(); } }
直接調用,結果如下:
下面是爬取天貓上商品信息的代碼,其實跟爬取京東的代碼差不多,只是根據頁面的html代碼進行了修改:
public void downTmallProduct() throws IOException { String input = "辣條"; // 需要爬取商品信息的網站地址 String url = "https://list.tmall.com/search_product.htm?q=" + input; // 動態模擬請求數據 HttpGet httpGet = new HttpGet(url); CloseableHttpClient httpclient = HttpClients.createDefault(); // 模擬瀏覽器瀏覽(user-agent的值可以通過瀏覽器瀏覽,查看發出請求的頭文件獲取) httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"); CloseableHttpResponse response = httpclient.execute(httpGet); // 獲取響應狀態碼 int statusCode = response.getStatusLine().getStatusCode(); try { HttpEntity entity = response.getEntity(); if (statusCode == 200) { String html = EntityUtils.toString(entity, Consts.UTF_8); Document doc = null; doc = Jsoup.parse(html); Elements ulList = doc.select("div[class='view grid-nosku']"); Elements liList = ulList.select("div[class='product']"); for (Element item : liList) { // 商品ID String id = item.select("div[class='product']").select("p[class='productStatus']").select("span[class='ww-light ww-small m_wangwang J_WangWang']").attr("data-item"); System.out.println("商品ID:" + id); // 商品名稱 String name = item.select("p[class='productTitle']").select("a").attr("title"); System.out.println("商品名稱:" + name); // 商品價格 String price = item.select("p[class='productPrice']").select("em").attr("title"); System.out.println("商品價格:" + price); // 商品網址 String goodsUrl = item.select("p[class='productTitle']").select("a").attr("href"); System.out.println("商品網址:" + goodsUrl); // 商品圖片網址 String imgUrl = item.select("div[class='productImg-wrap']").select("a").select("img").attr("data-ks-lazyload"); System.out.println("商品圖片網址:" + imgUrl); System.out.println("------------------------------------"); } // 消耗掉實體 EntityUtils.consume(response.getEntity()); } else { // 消耗掉實體 EntityUtils.consume(response.getEntity()); } } finally { response.close(); } }
結果: