【头歌】旅游网站大数据分析-数据抓取

第1关 : 利用Jsoup抓取携程旅游网的数据

任务描述

本关任务:使用 Jsoup 获取携程旅游网的数据。

相关知识

现在我们目标是获取携程旅游网的数据,然后将获取到的数据清洗,清洗一些无意义的数据,最后在存入到Hadoop中,这样我们就完成了数据获取、数据清洗、数据存储

现在我们要来一起完成第一步,数据获取,在我们知道一个网站地址的前提下,如何提取该网站的数据为我们所用呢?

需要一些工具,比如 Jsoup

Jsoup 的使用

jsoup 是一款 JavaHTML 解析器,可直接解析某个 URL 地址、 HTML 文本内容。它提供了一套非常省力的 API ,可通过 DOMCSS 以及类似于 jQuery 的操作方法来取出和操作数据。

jsoup 的主要功能如下:

  • 从一个 URL 件或字符串中解析 HTML

  • 使用 DOMCSS 选择器来查找、取出数据;

  • 可操作 HTML 元素、属性、文本;

jsoup 是基于 MIT 协议发布的,可放心使用于商业项目。

package step1;
import java.io.File;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Task {
	/**
	 * @param filePath	文件路径:backups/www.ctrip.com.txt/
	 * @return
	 * @throws IOException
	 */

	public Document getHtml1(String url) throws IOException{
           
        Document document = Jsoup.parse( new File( "./backups/www.ctrip.com.txt" ) , "utf-8" );
		   // System.out.println(document.title());
		   // System.out.println(document);
        
		return document;
	} 
	/**
	 * 
	 * @param url	网址http://hotels.ctrip.com/domestic-city-hotel.html
	 * @return
	 * @throws IOException
	 */
	public Document getHtml2(String url) throws IOException{
        Document document = Jsoup.parse( new File( "./backups/hotels.ctrip.com_domestic-city-hotel.txt" ) , "utf-8" );
        //System.out.println(document.title());
		return document;
	} 


}

第2关:解析并提取HTML 元素(一)

package step2;
import java.io.File;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Task {
    
 public Document getDoc1(String url) throws IOException{
      File file=new File("./backups/www.ctrip.com.txt");
       Document document =Jsoup.parse(file,"UTF-8","http://www.ctrip.com/");
    
       return document ;
    }

    //获取“http://you.ctrip.com/”的Docment对象
    public Document getDoc2(String url) throws IOException{
        File file=new File("./backups/you.ctrip.com.txt");
        Document document =Jsoup.parse(file,"UTF-8","http://you.ctrip.com");
        
        return document ;
    }


    //获取所有链接

    public Elements getLinks(Document doc){
       Elements links=doc.select("link[href]");
        return links;
    }
    
    //获取第一个class为“pop_attention”的div
    public Element getDiv(Document doc){
       Element element =doc.select("div.pop_attention").first();
        return element ;
    }
    
    //获取所有li之后的i标签
    public Elements getI(Document doc){
     Elements element =doc.select("li>i");
        return element ;
    }

}

第3关:解析并提取HTML 元素(二)

package step3;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Task {
   
   //通过filePath文件路径获取Docment对象
   public Document getDoc(String filePath) throws IOException{
   	/**********   Begin   **********/
File file=new File("./backups/hotel.ctrip.com.txt");
      Document doc=Jsoup.parse(file,"UTF-8","http://hotels.ctrip.com/");
       return doc;
   	/**********   End   **********/
   }

   //获取所有链接
   public List<String> getLinks(Document doc){
   	/**********   Begin   **********/
       List<String> ar=new ArrayList<>();
       Elements kk=doc.select("a[href]");
       for(Element gg:kk){
          ar.add(gg.tagName()+"$"+gg.attr("abs:href")+"("+gg.text()+")"); 
       } 
       return ar; 
   	/**********   End   **********/
   }
   
   //获取图片
   public List<String> getMedia(Document doc){
   	/**********   Begin   **********/
      List<String> list=new ArrayList<>(); 
       Elements ll=doc.select("[src]"); 
       for(Element h:ll){ 
           if(h.tagName().equals("img")){ 
               list.add(h.tagName()+"$"+h.attr("abs:src"));
           }
       }
       
       return list;
   	/**********   End   **********/
   }
   
   //获取link[href]链接
   public List<String> getImports(Document doc){
   	/**********   Begin   **********/
List<String> list=new ArrayList<>();
       Elements kk=doc.select("link[href]");
       for(Element g:kk){
             list.add(g.tagName()+"$"+g.attr("abs:href")+"("+g.attr("rel")+")");
       }
       
       return list;
   	/**********   End   **********/
   }
   
}

第4关:使用Jsoup抓取携程旅游网全国城市信息

package step4;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Task {
    
   public Document getDoc(String url) throws IOException{
        
      File file=new File("backups/hotels.ctrip.com_domestic-city-hotel.txt");
       Document doc=Jsoup.parse(file,"UTF-8","http://hotels.ctrip.com/");
        return doc;
	}
    
    /**
	 * 获取所有城市返回城市信息集合
	 * @param doc	
	 * @return
	 */
	public List<HotelCity> getAllCitys(Document doc){
       List<HotelCity> cities = new ArrayList<HotelCity>(); 
        
Elements aa= doc.getElementsByClass("pinyin_filter_detail layoutfix");
        Element pp = aa.first();
        Elements hh= pp.getElementsByTag("dd");
        Elements hts=pp.getElementsByTag("dt");
        
       for (int i = 0; i < hh.size(); i++) {
         Element bb = hts.get(i);
        Element head_hotelsLink = hh.get(i);
     Elements links = head_hotelsLink.children();
           
        for (Element link : links) {
                String pinyin_cityId = link.attr("href").replace("/hotel/", "");
                String pinyin = pinyin_cityId.replace(StringUtil.getNumbers(link.attr("href")), "");//截取拼音
                HotelCity city = new HotelCity();
                city.setCityId(StringUtil.getNumbers(link.attr("href"))); //截取cityId
                city.setCityName(link.text());
                city.setHeadPinyin(bb.text());
                city.setPinyin(pinyin);
                cities.add(city);

    }
		
	}
        return cities;
    }

}

  • 12
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卷心菜yst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值