2-1-9

package task;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Crawler {
    
    //1.成员变量
    private static String outputfile = "output\\长沙二手房房源数据.txt";
    private static String sourcefile = "resource\\长沙二手房房源.htm";
    
    
    //2.解析本地网页,返回Document
    public static Document getHtml() throws IOException {
        File f=new File(sourcefile);  //创建文件对象
        //解析本地文件,第一个参数是文件对象
        //第二个参数是网页的字符编码格式
        Document doc=Jsoup.parse(f,"utf-8");  
        return doc;
    }
    
    //3.提取数据,返回集合
    public static ArrayList<EntityBean> getData(Document doc) throws IOException {
        Elements elements=doc.select("#content > div.leftContent > ul > li > div.info.clear");
        ArrayList<EntityBean> list=new ArrayList<EntityBean>();//创建集合对象
        for(Element e :elements ) {  //10部电影信息
            String bt=e.select("[class=title]").text();
            String wz=e.select("[class=positionInfo]").text();
            String fwxx=e.select("[class=houseInfo]").text();
            String zj=e.select("[class=totalPrice]").text();
            String dj=e.select("[class=unitPrice]").text();
            //System.out.println(bt+" "+wz+" "+fwxx+" "+zj+" "+dj);
            EntityBean eb=new EntityBean(bt,wz,fwxx,zj,dj);
            list.add(eb);
        }
        return list;
    }
    
    //4.写出到本地文件系统
    public static void wirteToFile(ArrayList<EntityBean>  list) throws FileNotFoundException {
        PrintWriter pw=new PrintWriter(outputfile);//创建字节打印流对象
        for(EntityBean eb : list) {  //10
            pw.println(eb.getBt()+"\t"+eb.getWz()+"\t"+eb.getFwxx()+
                    "\t"+eb.getZj()+"\t"+eb.getDj());
        }
        pw.close();
    }
    //5.主方法
    public static void main(String[] args) throws IOException {
        Document doc = Crawler.getHtml();//解析网页,得到doc对象
        System.out.println("网页标题:"+doc.title());
        ArrayList<EntityBean> entitys = Crawler.getData(doc);  //提取数据
        Crawler.wirteToFile(entitys);
    }
}
package task;

public class EntityBean {
    //成员变量列表
    private String bt;
    private String wz;
    private String fwxx;
    private String zj;
    private String dj;
    
    //get()方法和set()方法
    public String getBt() {
        return bt;
    }
    public void setBt(String bt) {
        this.bt = bt;
    }
    public String getWz() {
        return wz;
    }
    public void setWz(String wz) {
        this.wz = wz;
    }
    public String getFwxx() {
        return fwxx;
    }
    public void setFwxx(String fwxx) {
        this.fwxx = fwxx;
    }
    public String getZj() {
        return zj;
    }
    public void setZj(String zj) {
        this.zj = zj;
    }
    public String getDj() {
        return dj;
    }
    public void setDj(String dj) {
        this.dj = dj;
    }
    
    //有参构造方法
    public EntityBean(String bt, String wz, String fwxx, String zj, String dj) {
        super();
        this.bt = bt;
        this.wz = wz;
        this.fwxx = fwxx;
        this.zj = zj;
        this.dj = dj;
    }
    
    //无参构造方法
    public EntityBean() {
        super();
    }
    
    //重写toString()方法
    @Override
    public String toString() {
        return "EntityBean [bt=" + bt + ", wz=" + wz + ", fwxx=" + fwxx + ", zj=" + zj + ", dj=" + dj + "]";
    }
    
}

 

posted @ 2022-09-13 14:58  aq阿桂  阅读(24)  评论(0)    收藏  举报