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

2、对得到的地址进行分析,把一些看起来多余的地址参数删掉重新请求(具体的其他参数我目前还没有深入研究),发现并没有对页面的访问产生影响。再对网址URL进行解码,得到%C3%AB%BD%ED为中文“毛巾”的UrlEncode编码。

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

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运行结果:


--------------------- 
作者:会编程的耗子 
来源:CSDN 
原文:https://blog.csdn.net/qq_32024351/article/details/84823414 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/yyongsheng/article/details/88822760