天天看點

Java爬蟲項目(一)利用Jsoup爬蟲爬取天貓商品資訊

前言 

這是我第一次用Java來寫爬蟲項目,研究的也不是很透徹,是以爬蟲技術的理論方面的就不說太多了。 主要還是以如何爬取商品資訊為主,爬取最簡單的商品資訊,給出大概的思路和方法。

對于沒有反爬技術的網站,爬取商品資訊最簡單。我測試了京東、淘寶、天貓這些大型購物網站,發現隻有天貓商城是沒有做任何反爬處理的,是以就從最簡單的爬取天貓商品資訊開始寫。

思路方法

1、對于沒有反爬技術的網站思路最簡單。直接在天貓商城首頁https://www.tmall.com/搜尋“毛巾”時,會向一個伺服器送出請求,得到跳轉後的頁面:

https://list.tmall.com/search_product.htm?q=%C3%AB%BD%ED&type=p&vmarket=&spm=875.7931836%2FB.a2227oh.d100&from=mallfp..pc_1_searchbutton

Java爬蟲項目(一)利用Jsoup爬蟲爬取天貓商品資訊

2、對得到的位址進行分析,把一些看起來多餘的位址參數删掉重新請求(具體的其他參數我目前還沒有深入研究),發現并沒有對頁面的通路産生影響。再對網址URL進行解碼,得到%C3%AB%BD%ED為中文“毛巾”的UrlEncode編碼。

https://list.tmall.com/search_product.htm?q=%C3%AB%BD%ED

Java爬蟲項目(一)利用Jsoup爬蟲爬取天貓商品資訊

3、以上分析則可以得出該頁面的請求位址為https://list.tmall.com/search_product.htm?q=毛巾

4、Java背景代碼

<!-- 爬蟲相關Jar包依賴 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.10-FINAL</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.11.3</version>
</dependency>
           
String input = "毛巾";
// 需要爬取商品資訊的網站位址
String url = "https://list.tmall.com/search_product.htm?q=" + input;
// 動态模拟請求資料
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 = null;
        // doc擷取整個頁面的所有資料
        doc = Jsoup.parse(html);
        //輸出doc可以看到所擷取到的頁面源代碼
//      System.out.println(doc);
        // 通過浏覽器檢視商品頁面的源代碼,找到資訊所在的div标簽,再對其進行一步一步地解析
        Elements ulList = doc.select("div[class='view grid-nosku']");
        Elements liList = ulList.select("div[class='product']");
        // 循環liList的資料(具體擷取的資料值還得看doc的頁面源代碼來擷取,可能稍有變動)
        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();
    }
           

5、Java運作結果:

Java爬蟲項目(一)利用Jsoup爬蟲爬取天貓商品資訊