同步第三方IP地址库到本地的IP地址库

随笔

2017-03-13

1313

0

一、下载中国IP网段

下载地址http://ipblock.chacuo.net/view/c_CN

二、寻找第三方IP地址库

目前博主发现免费的,比较好用的有

太平洋:http://whois.pconline.com.cn/

淘宝:http://ip.taobao.com

三、编写代码(核心代码)

public class AnalysisIP {
    // 模拟UA
    public static String UA = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.28 (KHTML, like Gecko) Chrome/26.0.1397.2 Safari/537.28";

    @Test
    public void analysis() {

        CommonParm.count = 0;
        // 从文本中获取ip网段
        InputStream in = this.getClass()
                .getResourceAsStream("/CN-20170313.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        DefaultHttpClient hc = new DefaultHttpClient();
        MongoDatabase mongo = MongoUtil.getMongoDatabase();
        // 太平洋IP数据库
        MongoCollection<Document> ipColl = mongo.getCollection("ip_pconline");
        InputStream is = null;
        String str = null;
        int count = 0;
        try {
            while (true) {
                str = reader.readLine();
                if (str == null) {
                    break;
                }
                // 获取起始IP段,和终止IP段
                String[] ip = str.split("\t");
                if (ip.length > 2) {
                    String[] ip1 = ip[0].split("\\.");
                    String[] ip2 = ip[1].split("\\.");
                    // 循环获取ip
                    for (int i = Integer.parseInt(ip1[0]); i <= Integer
                            .parseInt(ip2[0]); i++) {
                        for (int ii = Integer.parseInt(ip1[1]); ii <= Integer
                                .parseInt(ip2[1]); ii++) {
                            for (int iii = Integer.parseInt(ip1[2]); iii <= Integer
                                    .parseInt(ip2[2]); iii++) {
                                CommonParm.count++;
                                // System.out.println(i+"."+ii+"."+iii+".0");
                                HttpGet http_get = new HttpGet(
                                        "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip="
                                                + i + "." + ii + "." + iii
                                                + "." + "0");
                                http_get.setHeader("User-Agent", UA);
                                HttpEntity entity;
                                entity = hc.execute(http_get).getEntity();
                                is = entity.getContent();
                                StringWriter sw = new StringWriter();
                                IOUtils.copy(is, sw, Charset.forName("gbk"));
                                is.close();
                                String res = sw.toString();
                                Document doc = JSON.parseObject(res,
                                        Document.class);
                                Bson ipBson = Filters.eq("_id", i + "." + ii
                                        + "." + iii + "." + "0");
                                System.out.println(doc);
                                ipColl.replaceOne(ipBson, doc);
                                Thread.sleep(500);
                                CommonParm.count++;
                            }
                        }
                    }
                }
                // System.out.println(str);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

转载请注明出处: http://www.julyme.com/20170313/72.html

发表评论

全部评论:0条

Julyme

感觉还行吧。

Julyme的IT技术分享



/sitemap