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\\2018年世界各国人口总数数据.txt";
private static String sourcefile = "resource\\2018年世界各国人口总数统计.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 {
//利用selector选择器,提取32行数据
Elements trs=doc.select("body > div.container > div.container > div:nth-child(4) > div.col-md-9 > div > div > div > table > tbody > tr");
ArrayList<EntityBean> list=new ArrayList<EntityBean>();//创建集合对象
for(Element tr : trs) {
if(tr.childNodeSize()<5) {
continue;
}
//获取tr,每一行的前6列数据
String gj=tr.child(1).text();
String szz=tr.child(2).text();
String rk=tr.child(3).text();
String zb=tr.child(4).text();
//每行数据创建一个对应的实例化对象ed
EntityBean eb=new EntityBean(gj,szz,rk,zb);
//System.out.println(eb.toString());
list.add(eb);//将每一行数据对象放进集合
}
return list;
}
//4.写出到本地文件系统
public static void wirteToFile(ArrayList<EntityBean> ebs) throws FileNotFoundException {
PrintWriter pw=new PrintWriter(outputfile);//创建字节打印流对象
for(EntityBean eb : ebs) {
pw.println(eb.getGj()+"\t"+eb.getSzz()+"\t"+eb.getRk()+
"\t"+eb.getZb());
}
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 gj;
private String szz;
private String rk;
private String zb;
//get()方法和set()方法
public String getGj() {
return gj;
}
public void setGj(String gj) {
this.gj = gj;
}
public String getSzz() {
return szz;
}
public void setSzz(String szz) {
this.szz = szz;
}
public String getRk() {
return rk;
}
public void setRk(String rk) {
this.rk = rk;
}
public String getZb() {
return zb;
}
public void setZb(String zb) {
this.zb = zb;
}
//有参构造方法
public EntityBean(String gj, String szz, String rk, String zb) {
super();
this.gj = gj;
this.szz = szz;
this.rk = rk;
this.zb = zb;
}
//无参构造方法
public EntityBean() {
super();
}
//重写toString()方法
@Override
public String toString() {
return "EntityBean [gj=" + gj + ", szz=" + szz + ", rk=" + rk + ", zb=" + zb + "]";
}
}