欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java获取沈阳市公交数据,java获取沈阳市,package util

来源: javaer 分享于  点击 27991 次 点评:12

Java获取沈阳市公交数据,java获取沈阳市,package util


package util;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;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;/** * 沈阳市公交数据爬虫 * @author Administrator * */public class Grab {    List<String> l = new ArrayList<String>();    public List<String> grab(String url,String feature) {        try {            Document doc = Jsoup.connect(url).get();            Elements elements = doc.select(feature);            for (Element element : elements) {                String name = null;                if ("".equals(element.attr("title"))) {                    name = element.text();                } else {                    name = element.attr("title");                }                l.add(name);            }        } catch (IOException e) {            e.printStackTrace();        }        return l;    }    public List<String> grabAll(){        String feature = ".ChinaTxt dd a";        String str[] = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L",                "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z",                "1", "2", "5" };        List<String> lUrl = new ArrayList<String>();        for (String string : str) {            lUrl.add("http://bus.mapbar.com/shenyang/station_list/" + string                    + ".shtml");        }        for (String url : lUrl) {            this.grab(url, feature);        }        return l;    }    //转化为mysql的insert语句    public List<String> convertToMysql(){        List<String> lTemp=this.grabAll();        List<String> lSQL=new ArrayList<String>();        for (String string : lTemp) {            lSQL.add("insert into tab_bussite(name)values(\\""+string+"\\");");        }        return lSQL;    }    public static void main(String[] args) {        Grab grab=new Grab();        List<String> insertSqls=grab.convertToMysql();//      for (String string : insertSqls) {//          System.out.println(string);//      }//      System.out.println(insertSqls.size());        File file=new File("xxx\\\\mysql.sql");//写输出路径        FileOutputStream fos=null;        try {            fos=new FileOutputStream(file);            for (String string : insertSqls) {                fos.write((string+"\\n").getBytes());                fos.flush();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally{            if(fos!=null){                try {                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}//该片段来自于http://byrx.net
相关栏目:

用户点评