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

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 input = new File("backups/www.ctrip.com.txt/");
        Document doc = Jsoup.parse(input,"UTF-8","http://www.educoder.net/");
        
		return doc;
		/**********   End   **********/
	} 

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

		return doc;
		/**********   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 input = new File("./backups/www.ctrip.com.txt"); 
        Document doc = Jsoup.parse(input,"UTF-8","http://www.educoder.net/");
        // Document doc=Jsoup.connect(url).get();
        return doc;
        /**********   End   **********/
    }
    
    public Document getDoc2(String filePath) throws IOException{
        /**********   Begin   **********/
        File input = new File("./backups/you.ctrip.com.txt"); 
        Document doc = Jsoup.parse(input,"UTF-8","http://www.educoder.net/");
        // Document doc=Jsoup.connect(url).get();
        return doc;
        /**********   End   **********/
    }
    //获取所有链接
    public Elements getLinks(Document doc){
        /**********   Begin   **********/
        return doc.select("link[href]");    
        /**********   End   **********/
    }
    
    //获取第一个class为“pop_attention”的div
    public Element getDiv(Document doc){
        /**********   Begin   **********/
        Element element = doc.getElementsByClass("pop_attention").first();
        //Element element = doc.select("div.pop_attention").first();
        return element;    
        /**********   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   **********/
        File input = new File("./backups/hotel.ctrip.com.txt");
        Document doc = Jsoup.parse(input,"UTF-8","http://www.educoder.net/");
        
		return doc;
		/**********   End   **********/
	}

	//获取所有链接
	public List<String> getLinks(Document doc){
        /**********   Begin   **********/
        List<String> arrays=new ArrayList<>();
        Elements links = doc.select("a[href]");
        for (Element link : links) {
            arrays.add(link.tagName()+"$"+link.attr("abs:href")+"("+link.text()+")");
        }
        return arrays;
        /**********   End   **********/
	}
	
	//获取图片
	public List<String> getMedia(Document doc){
        /**********   Begin   **********/
         List<String> arrays=new ArrayList<>();
        Elements media = doc.select("[src]");
        for (Element src : media) {
            if (src.tagName().equals("img")){
                arrays.add(src.tagName()+"$"+src.attr("abs:src"));
            }
        }
        return arrays;
	}
	
	//获取link[href]链接
	public List<String> getImports(Document doc){
        /**********   Begin   **********/
        List<String> arrays=new ArrayList<>();
        Elements imports = doc.select("link[href]");
        for (Element link : imports) {
            arrays.add(link.tagName()+"$"+link.attr("abs:href")+"("+link.attr("rel")+")");
        }
        return arrays;
        /**********   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 input = new File("./backups/hotels.ctrip.com_domestic-city-hotel.txt");
        Document doc = Jsoup.parse(input,"UTF-8","http://www.educoder.net/");
        
		return doc;
		/**********   End   **********/
	}
    
	/**
	 * 获取所有城市返回城市信息集合
	 * @param doc	
	 * @return
	 */
	public List<HotelCity> getAllCitys(Document doc){
		/**********   Begin   **********/
        List<HotelCity> cities = new ArrayList<HotelCity>();
        Elements pinyin_filter_elements = doc.getElementsByClass("pinyin_filter_detail layoutfix");//访问网址检查发现所有数据存在该标签中
        //确保拿到的是第一个包含所有城市的Element
        Element pinyin_filter = pinyin_filter_elements.first();
        //拼音首字符Elements
        Elements pinyins = pinyin_filter.getElementsByTag("dt");
        //所有dd的Elements
        Elements hotelsLinks = pinyin_filter.getElementsByTag("dd");

        for (int i = 0; i < hotelsLinks.size(); i++) {
            //当前字母
            Element head_pinyin = pinyins.get(i);
            //当前dd
            Element head_hotelsLink = hotelsLinks.get(i);
            //当前dd下的所有孩子也就是a标签
            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(head_pinyin.text());
                city.setPinyin(pinyin);
                cities.add(city);
            }        
        }

		return cities;
		/**********   End   **********/
	}
}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐