Java获取手机号码归属地

 工作过程中抓取了很多手机号码,现需要获取他们的归属地。于是自己写了个工具类,这个工具类只是基本了实现该功能,没有考虑其他因素。

      我知道手机号码归属地的判断是根据前7位,一开始本想获取他们的规律,就是哪个号码段的手机是属于哪个地区,但发现自己无法获取。于是想到了有一些网站有这个功能,同时自己也写爬虫,于是利用爬虫的思路去完成这个功能。这里大概思路是通过HttpClient去模拟提交那些网站的查询功能,这里是www.ip138.com,然后通过正则表达式去解析HttpClient相应内容,从里面抽取出手机归属地。同时对要查询的手机进行一个验证,具体代码请看如下:

   

Java代码   收藏代码
  1. import java.util.regex.Matcher;  
  2. import java.util.regex.Pattern;  
  3.   
  4. import org.apache.commons.httpclient.HttpClient;  
  5. import org.apache.commons.httpclient.NameValuePair;  
  6. import org.apache.commons.httpclient.methods.PostMethod;  
  7. import org.apache.commons.httpclient.params.HttpMethodParams;  
  8. /** 
  9.  * 通过手机号码,获得该号码的归属地 
  10.  *  
  11.  * @author Administrator 
  12.  * 
  13.  */  
  14. public class MobileFromUtil {  
  15.     //正则表达式,抽取手机归属地  
  16.     public static final String REGEX_GET_MOBILE=  
  17.         "(?is)(<tr[^>]+>[\\s]*<td[^>]+>[\\s]*卡号归属地[\\s]*</td>[\\s]*<td[^>]+>([^<]+)</td>[\\s]*</tr>)"//2:from  
  18.     //正则表达式,审核要获取手机归属地的手机是否符合格式,可以只输入手机号码前7位  
  19.     public static final String REGEX_IS_MOBILE=  
  20.         "(?is)(^1[3|4|5|8][0-9]\\d{4,8}$)";  
  21.       
  22.     /** 
  23.      * 获得手机号码归属地 
  24.      *  
  25.      * @param mobileNumber 
  26.      * @return 
  27.      * @throws Exception 
  28.      */  
  29.     public static String getMobileFrom(String mobileNumber) throws Exception {  
  30.         if(!veriyMobile(mobileNumber)){  
  31.             throw new Exception("不是完整的11位手机号或者正确的手机号前七位");  
  32.         }  
  33.         HttpClient client=null;  
  34.         PostMethod method=null;  
  35.         NameValuePair mobileParameter=null;  
  36.         NameValuePair actionParameter=null;  
  37.         int httpStatusCode;  
  38.           
  39.         String htmlSource=null;  
  40.         String result=null;  
  41.           
  42.           
  43.         try {  
  44.             client=new HttpClient();  
  45.             client.getHostConfiguration().setHost("www.ip138.com"8080"http");  
  46.             method=new PostMethod("/search.asp");  
  47.             mobileParameter=new NameValuePair("mobile",mobileNumber);  
  48.             actionParameter=new NameValuePair("action","mobile");  
  49.             method.setRequestBody(new NameValuePair[] { actionParameter,mobileParameter });   
  50.             //设置编码  
  51.             method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GB2312");  
  52.               
  53.             client.executeMethod(method);  
  54.             httpStatusCode=method.getStatusLine().getStatusCode();  
  55.             if(httpStatusCode!=200){  
  56.                 throw new Exception("网页内容获取异常!Http Status Code:"+httpStatusCode);  
  57.             }  
  58.               
  59.             htmlSource=method.getResponseBodyAsString();  
  60.             if(htmlSource!=null&&!htmlSource.equals("")){  
  61.                 result=parseMobileFrom(htmlSource);  
  62.             }  
  63.         } catch (RuntimeException e) {  
  64.             // TODO Auto-generated catch block  
  65.             e.printStackTrace();  
  66.         }finally{  
  67.             method.releaseConnection();  
  68.         }  
  69.           
  70.         return result;  
  71.           
  72.     }  
  73.   
  74.   
  75.     /** 
  76.      * 从www.ip138.com返回的结果网页内容中获取手机号码归属地,结果为:省份 城市 
  77.      *  
  78.      * @param htmlSource 
  79.      * @return 
  80.      */  
  81.     public static String parseMobileFrom(String htmlSource){  
  82.         Pattern p=null;  
  83.         Matcher m=null;  
  84.         String result=null;  
  85.           
  86.         p=Pattern.compile(REGEX_GET_MOBILE);  
  87.         m=p.matcher(htmlSource);  
  88.           
  89.         while(m.find()){  
  90.             if(m.start(2)>0){  
  91.                 result=m.group(2);  
  92.                 result=result.replaceAll("&nbsp;"" ");  
  93.             }  
  94.         }  
  95.           
  96.           
  97.         return result;  
  98.           
  99.     }  
  100.       
  101.     /** 
  102.      * 验证手机号 
  103.      * @param mobileNumber 
  104.      * @return 
  105.      */  
  106.     public static boolean veriyMobile(String mobileNumber){  
  107.         Pattern p=null;  
  108.         Matcher m=null;  
  109.           
  110.         p=Pattern.compile(REGEX_IS_MOBILE);  
  111.         m=p.matcher(mobileNumber);  
  112.           
  113.         return m.matches();  
  114.     }  
  115.       
  116.     /** 
  117.      * 测试 
  118.      * @param args 
  119.      * @throws Exception  
  120.      */  
  121.     public static void main(String[] args) throws Exception {  
  122.         System.out.println(getMobileFrom("13888888888"));  
  123.     }  
  124.   
  125. }  
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
根据提供的引用内容,我们可以通过获取请求IP的方式来获取基于手机号的IP地址。具体步骤如下: 1.在controller中添加HttpServletRequest请求参数,获取请求IP地址。 2.使用手机号码归属地查询API,将手机号码作为参数发送请求,获取返回的JSON数据。 3.解析JSON数据,获取IP地址信息。 下面是一个Java获取基于手机号的IP地址的示例代码: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class IPController { @RequestMapping("/getIPByPhone") public String getIPByPhone(HttpServletRequest request) { String phone = "手机号码"; String ip = ""; try { // 获取请求IP地址 String requestIP = request.getRemoteAddr(); // 发送请求获取归属地信息 URL url = new URL("http://api.showji.com/Locating/www.showji.com.aspx?output=json&callback=&m=" + phone); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/json"); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 解析JSON数据,获取IP地址信息 String result = response.toString(); int index = result.indexOf("cip"); if (index > 0) { ip = result.substring(index + 6, index + 20); } } catch (Exception e) { e.printStackTrace(); } return "IP地址:" + ip; } } ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值