全中國的省市縣鎮鄉村數據獲取以及展示java源代碼


第一步、准備工作(數據源+工具):

  數據源(截止目前最全面權威的官方數據):http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2013/

  爬取數據的工具(爬蟲工具):http://jsoup.org/

第二、數據源分析:

  首先jsoup工具的使用我在這里就不做講解了,感興趣的可以自己動手去查閱。

  做開發就應該多去了解一些軟件工具的使用,在平常開發過程中遇到了才知道從何下手,鼓勵大家多平時留意一些身邊的軟件工具,以備不時之需。在做 這個東西以前,我也不知道jsoup要怎么用,但我知道jsoup可以用來干嘛,在我需要的用到的時候,再去查閱資料,自己學習。

 

第三部、java源代碼:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

/**
 * 全國省市縣鎮村數據爬取
 * @author liushaofeng
 * @date 2015-10-11 上午12:19:39
 * @version 1.0.0
 */
public class JsoupTest
{
    private static Map<Integer, String> cssMap = new HashMap<Integer, String>();
    private static BufferedWriter bufferedWriter = null;

    static
    {
        cssMap.put(1, "provincetr");//
        cssMap.put(2, "citytr");//
        cssMap.put(3, "countytr");//
        cssMap.put(4, "towntr");//
        cssMap.put(5, "villagetr");//
    }

    public static void main(String[] args) throws IOException
    {
        int level = 1;

        initFile();

        // 獲取全國各個省級信息
        Document connect = connect("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2013/");
        Elements rowProvince = connect.select("tr." + cssMap.get(level));
        for (Element provinceElement : rowProvince)// 遍歷每一行的省份城市
        {
            Elements select = provinceElement.select("a");
            for (Element province : select)// 每一個省份(四川省)
            {
                parseNextLevel(province, level + 1);
            }
        }

        closeStream();
    }

    private static void initFile()
    {
        try
        {
            bufferedWriter = new BufferedWriter(new FileWriter(new File("d:\\CityInfo.txt"), true));
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private static void closeStream()
    {
        if (bufferedWriter != null)
        {
            try
            {
                bufferedWriter.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
            bufferedWriter = null;
        }
    }

    private static void parseNextLevel(Element parentElement, int level) throws IOException
    {
        try
        {
            Thread.sleep(500);//睡眠一下,否則可能出現各種錯誤狀態碼
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        Document doc = connect(parentElement.attr("abs:href"));
        if (doc != null)
        {
            Elements newsHeadlines = doc.select("tr." + cssMap.get(level));//
            // 獲取表格的一行數據
            for (Element element : newsHeadlines)
            {
                printInfo(element, level + 1);
                Elements select = element.select("a");// 在遞歸調用的時候,這里是判斷是否是村一級的數據,村一級的數據沒有a標簽
                if (select.size() != 0)
                {
                    parseNextLevel(select.last(), level + 1);
                }
            }
        }
    }

    /**
     * 寫一行數據到數據文件中去
     * @param element 爬取到的數據元素
     * @param level 城市級別
     */
    private static void printInfo(Element element, int level)
    {
        try
        {
            bufferedWriter.write(element.select("td").last().text() + "{" + level + "}["
                + element.select("td").first().text() + "]");
            bufferedWriter.newLine();
            bufferedWriter.flush();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private static Document connect(String url)
    {
        if (url == null || url.isEmpty())
        {
            throw new IllegalArgumentException("The input url('" + url + "') is invalid!");
        }
        try
        {
            return Jsoup.connect(url).timeout(100 * 1000).get();
        } catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }
}

第三步、插入數據到DB:

按照一定的數據結構插入到對應的表中即可

 

最后效果圖:

www.10086bank.com

 

 

以上!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM