Jsoup的学习(二)_案例

  |  

需要学习对页面的数据进行爬取,以下内容仅供学习demo,非商业用途。

案例

举例一

举例:获取携程的酒店名称


1
2
3
4
5
6
7
8
9
10
11
@Test
public void test1(){
String url = "http://hotels.ctrip.com/hotel/701612.html";
Document document = Jsoup.connect(url)
.timeout(10000)
.referrer("http://hotels.ctrip.com")
.get();
System.out.println(document);
Elements elements = document.select(".cn_n");
System.out.println("酒店名称:"+elements.text());
}

  • 输出

    1
    2
    3
    4
    5
    6
    7
    8
    9
    ...
    <div class="htl_info" id="J_htl_info">
    <div class="name" itemtype="//schema.org/Hotel">
    <h2 class="cn_n" itemprop="name">河南大厦</h2>
    <h2 class="en_n">Beijing Henan Plaza Hotel</h2>
    <span id="ctl00_MainContentPlaceHolder_commonHead_span_hotel_medal" data-role="title" class="medal " title="" rstar="2" cpr="0"></span>
    <span class="medal ico_quality_gold" title="确认订单更快速,入住过程更顺利,携程服务品质认证。" style="display:none" id="J_ServiceScoreIcon">品质保障</span>
    </div>
    ...
  • 通过标签选择器定位就可以了,效果和element可以看上图

    1
    酒店名称:河南大厦

举例二

举例:获取酒店详情中房型的价格

寻找url
  • 由于酒店房型数据是Ajax的,需要在开发者工具中(F12)寻找url
    1
    2
    3
    4
    5
    6
    7
    原url:http://hotels.ctrip.com/Domestic/tool/AjaxHote1RoomListForDetai1.aspx?psid=&MasterHotelID=701612&hotel=701612&EDM=F&roomId=&IncludeRoom=&city=1&showspothotel=T&supplier=&IsDecoupleSpotHotelAndGroup=F&contrast=0&brand=0&startDate=2018-12-10&depDate=2018-12-11&IsFlash=F&RequestTravelMoney=F&hsids=&IsJustConfirm=&contyped=0&priceInfo=-1&equip=&filter=&productcode=&couponList=&abForHuaZhu=&defaultLoad=T&esfiltertag=&estagid=&Currency=&Exchange=&TmFromList=F&RoomGuestCount=1,1,0&eleven=1a82f6826ef6f79f8b687d200c8bc26bc83f5fd6e22ce188fa46c37e596ada0f&callback=CASZypaSpRGHqDDaCsI&_=1544406910108

    //然后进行测试缩小请求参数范围

    最后url:http://hotels.ctrip.com/Domestic/tool/AjaxHote1RoomListForDetai1.aspx?MasterHotelID=701615&hotel=701615&startDate=2018-12-10&depDate=2018-12-11

    //范围只需要改动酒店id和入住时间即可
设置请求头参数
  • 如果对请求的来源做了判断就需要在header设置referrer参数
寻找规律
  • 每一行的价格都是放在tr标签中的其中一个td中,仔细观察里面有span标签有base_price属性,直接属性选择器定位获取text,后续在正则取值。
1
2
3
4
5
6
7
8
9
@Test
public void test2(){
String url2 = "http://hotels.ctrip.com/Domestic/tool/AjaxHote1RoomListForDetai1.aspx?MasterHotelID=701615&hotel=701615&startDate=2018-12-10&depDate=2018-12-11";
Document document = Jsoup.connect(url2)
.timeout(10000)
.referrer("http://hotels.ctrip.com").get();
Elements select2 = element.select("tr td .base_price");
System.out.println(select2.text());
}
  • 输出
    1
    2
    3
    4
    ¥<\/dfn>1374<\/span><\/div>\u000a\u0009 \u000a\u0009\u0009\u000a\u0009\u0009\u000a\u0009<\/p>\u000a \u000a <\/span>\u000a<\/p>\u000a\u0009\u000a<\/td>\u000a
    ¥<\/dfn>942<\/span><\/div>\u000a\u0009 \u000a\u0009\u0009\u000a\u0009\u0009\u000a\u0009<\/p>\u000a \u000a <\/span>\u000a<\/p>\u000a\u0009\u000a<\/td>\u000a
    ¥<\/dfn>1140<\/span><\/div>\u000a\u0009 \u000a\u0009\u0009\u000a\u0009\u0009\u000a\u0009<\/p>\u000a \u000a <\/span>\u000a<\/p>\u000a\u0009\u000a<\/td>\u000a
    ¥<\/dfn>849<\/span><\/div>\u000a\u0009 \u000a\u0009\u0009\u000a\u0009\u0009\u000a\u0009<\/p>\u000a \u000a <\/span>\u000a<\/p>\u000a\u0009\u000a<\/td>\u000a
正则取值
  • 对dfn标签后的值进行匹配取值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    @Test
    public void test3(){
    String url2 = "http://hotels.ctrip.com/Domestic/tool/AjaxHote1RoomListForDetai1.aspx?MasterHotelID=701615&hotel=701615&startDate=2018-12-10&depDate=2018-12-11";
    Document document = Jsoup.connect(url2)
    .timeout(10000)
    .referrer("http://hotels.ctrip.com").get();
    Elements select = element.select("tr td .base_price");
    for (Element element1 : select) {
    String text = element1.text();
    //¥<\/dfn>1374<\/span><\/div>\u000a\u0009...
    Pattern pattern = Pattern.compile("dfn>(\\d+)<");
    Matcher matcher = pattern.matcher(text);
    if(matcher.find()){
    System.out.println(matcher.group(1));
    }
    }
    }
  • 输出

    1
    2
    3
    4
    1374
    942
    1140
    849

Copyright © 2018 - 2020 Kuanger All Rights Reserved.

访客数 : | 访问量 :