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

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 filePath) throws IOException{
		/**********   Begin   **********/
        File file = new File(filePath);
		Document d = Jsoup.parse(file, "UTF-8", "/backups/www.ctrip.com.txt/");
		return d;
		/**********   End   **********/
	} 

	/**
	 * 
	 * @param filePath	文件路径:backups/hotels.ctrip.com_domestic-city-hotel.txt/
	 * @return
	 * @throws IOException
	 */
	public Document getHtml2(String filePath) throws IOException{
        /**********   Begin   **********/
        File fl = new File(filePath);
		Document dt = Jsoup.parse(fl, "UTF-8", "/backups/hotels.ctrip.com_domestic-city-hotel.txt/");
		return dt;
		/**********   End   **********/
	} 
}

第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 {
    
	//通过filePath文件路径获取Docment对象
	public Document getDoc1(String filePath) throws IOException{
		/**********   Begin   **********/
        File file = new File(filePath);
		Document document = Jsoup.parse(file, "UTF-8","/backups/www.ctrip.com.txt");
		return document;
		/**********   End   **********/
	}

	
	public Document getDoc2(String filePath) throws IOException{
        /**********   Begin   **********/
		 File fe = new File(filePath);
		Document dt = Jsoup.parse(fe, "UTF-8","/backups/you.ctrip.com.txt");
		return dt;
		/**********   End   **********/
	}

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

 第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   **********/
		return Jsoup.parse(new File(filePath), "uft-8");
		/**********   End   **********/
	}

	//获取所有链接
	public List<String> getLinks(Document doc){
		/**********   Begin   **********/
		Elements select = doc.select("a[href]");
		List<String> list = new ArrayList<>();
		for (Element element : select){
			String temp = element.attr("href");
			if(!temp.startsWith("http")) temp = "http:" + temp;
			list.add(element.tagName() + "$" + temp + "(" + element.text() + ")");
		}
		return list;
		/**********   End   **********/
	}
	
	//获取图片
	public List<String> getMedia(Document doc){
		/**********   Begin   **********/
		Elements img = doc.select("img");
		List<String> list = new ArrayList<>();
		for (Element element : img){
			String temp = element.attr("src");
			if(!temp.startsWith("http")) temp = "http:" + temp;
			list.add(element.tagName() + "$" + temp);
		}
		return list;
		/**********   End   **********/
	}
	
	//获取link[href]链接
	public List<String> getImports(Document doc){
		/**********   Begin   **********/
		Elements link = doc.select("link");
		List<String> list = new ArrayList<>();
		for (Element value : link){
			String temp = value.attr("href");
			if(!temp.startsWith("http")) temp = "http:" + temp;
			list.add(value.tagName() + "$" + temp + "(" + value.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 {
    
    //通过filePath文件路径获取Docment对象
	public Document getDoc(String filePath) throws IOException{
        /**********   Begin   **********/
        File file = new File(filePath);
		Document doc = Jsoup.parse(file, "uft-8", "/backups/hotels.ctrip.com_domestic-city-hotel.txt");
		return doc;
		/**********   End   **********/
	}
    
	/**
	 * 获取所有城市返回城市信息集合
	 * @param doc	
	 * @return
	 */
	public List<HotelCity> getAllCitys(Document doc){
		/**********   Begin   **********/
		Elements select1 = doc.select("dl.layoutfix");
		List<HotelCity> list = new ArrayList<>();
        for (Element element : select1.select("a")){
			HotelCity hotelCity = new HotelCity();
			hotelCity.setCityId(element.attr("href").replaceAll("[^(0-9)]", ""));
			hotelCity.setCityName(element.text());
			hotelCity.setPinyin(element.attr("href").split("/")[2].replaceAll("[^(a-zA-Z)]",""));
			hotelCity.setHeadPinyin("A");
			list.add(hotelCity);
		}
        
		return list;
		/**********   End   **********/
	}
}

相关推荐

  1. 旅游网站数据分析 - 数据抓取

    2024-01-02 19:52:01       69 阅读
  2. :共享单车之数据分析

    2024-01-02 19:52:01       50 阅读
  3. 数字三角形

    2024-01-02 19:52:01       39 阅读
  4. 基于Python的旅游网站数据爬虫分析

    2024-01-02 19:52:01       37 阅读
  5. 爬虫技术抓取网站数据

    2024-01-02 19:52:01       41 阅读
  6. 爬虫技术抓取网站数据

    2024-01-02 19:52:01       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-02 19:52:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-02 19:52:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-02 19:52:01       82 阅读
  4. Python语言-面向对象

    2024-01-02 19:52:01       91 阅读

热门阅读

  1. 前端需要学GraphQL 吗?

    2024-01-02 19:52:01       60 阅读
  2. C++精进之路(十二)类和动态内存分配

    2024-01-02 19:52:01       49 阅读
  3. 91 两个数组的交集II

    2024-01-02 19:52:01       52 阅读
  4. 用g2o实现bundle adjustment

    2024-01-02 19:52:01       61 阅读
  5. 94. 二叉树的中序遍历(“颜色标记法”)

    2024-01-02 19:52:01       52 阅读
  6. 企微群发助手:企业微信营销的得力助手

    2024-01-02 19:52:01       59 阅读
  7. Web网页开发-初识web-笔记

    2024-01-02 19:52:01       58 阅读
  8. 如何使用版本控制工具(如Git)进行代码管理?

    2024-01-02 19:52:01       57 阅读
  9. uni-app、H5+ 下载并保存、打开文件

    2024-01-02 19:52:01       88 阅读