手头有5314个IP,大概有美国,台湾,香港的,我想知道他们都是哪里的!http://www.ip138.com/只能一个个的查询,太慢了!

解决方案 »

  1.   

    这个应该根据IP段进行处理。你应该先知道每个国家和地区的IP段
      

  2.   

    写个程序,将 5314 个 IP 放在数组内,循环提交给 ip138.com 查,再解析返回的 HTML 就可以了。
      

  3.   

    import java.io.IOException;
    import java.util.LinkedHashMap;
    import java.util.Map;import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.methods.PostMethod;public class IP {  public static void main(String[] args) throws HttpException, IOException {  
        Map<String, String> map = new LinkedHashMap<String, String>();
        map.put("211.100.26.124", "");
        map.put("133.13.233.11", "");
        map.put("209.131.36.158", "");
        map.put("138.175.248.10", "");
        map.put("60.12.227.64", "");
        map.put("72.21.206.80", "");
        map.put("61.152.173.36", "");
        long t0 = System.currentTimeMillis();
        int i = 0;
        int size = map.size();
        for (Map.Entry<String, String> entry : map.entrySet()) {
          System.out.printf("[%d/%d] 正在解析 %s", ++i, size, entry.getKey());
          String url = "http://www.ip138.com/ips8.asp";
          HttpClient client = new HttpClient();
          PostMethod method = new PostMethod(url);
          method.setParameter("action", "2");
          method.setParameter("ip", entry.getKey());
          client.executeMethod(method);      String response = method.getResponseBodyAsString();
          response = new String(response.getBytes("iso-8859-1"), "gb2312");
          response = response.substring(response.indexOf("本站主数据:"), response.indexOf("</li></ul>"));
          map.put(entry.getKey(), response);
          System.out.println(",解析完成");
          method.releaseConnection();
        }
        System.out.printf("解析 5314 个 IP 可能要花:%.2f 小时%n", (System.currentTimeMillis() - t0) * 5314 / (1000.0 * map.size()) / 3600);
        
        // 输出
        //for (Map.Entry<String, String> entry : map.entrySet()) {
        //  System.out.println(entry.getKey() + " --> " + entry.getValue());
        //}
        System.out.println("OK");
      }
    }以上代码,需要使用 Commons HttpClient 类库
    http://commons.apache.org/downloads/download_httpclient.cgi以及依赖类库:Commons Logging 和 Commons Codec 类库
    http://commons.apache.org/downloads/download_logging.cgi
    http://commons.apache.org/downloads/download_codec.cgi测试了一下解析 5314 个 IP 大约要花两个小时左右的时间。