package com.test; import java.io.IOException; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.httpclient.HttpException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; /*** * 使用Jsoup去解析查询手机号归属地 * 原理:抓取再解析html... * @author ljl * */ public class Test2 { /** * 正则表达式,抽取手机归属地 */ public static final String REGEX_GET_MOBILE = "(?is)(<tr[^>]+>[\\s]*<td[^>]+>[\\s]*卡号归属地[\\s]*</td>[\\s]*<td[^>]+>([^<]+)</td>[\\s]*</tr>)"; // 2:from /** * 正则表达式,审核要获取手机归属地的手机是否符合格式,可以只输入手机号码前7位 */ public static final String REGEX_IS_MOBILE = "(?is)(^1[3|4|5|8][0-9]\\d{4,8}$)"; /** * 从www.ip138.com * 返回的结果网页内容中获取手机号码归属地,结果为:省份 城市 * @param htmlSource * @return */ public static String parseMobileFrom(String htmlSource){ Pattern p=null; Matcher m=null; String result=null; p=Pattern.compile(REGEX_GET_MOBILE); m=p.matcher(htmlSource); while(m.find()){ if(m.start(2)>0){ result=m.group(2); result=result.replaceAll(" ", " "); } } return result; } /** * 验证手机号 * @param mobileNumber * @return */ public static boolean veriyMobile(String mobileNumber){ Pattern p=null; Matcher m=null; p=Pattern.compile(REGEX_IS_MOBILE); m=p.matcher(mobileNumber); return m.matches(); } public static void main(String[] args) throws Exception { String mobile="13800138000"; getNetFormMobileInfo(mobile); } private static void getNetFormMobileInfo(String mobileNumber) throws IOException, HttpException { if(!veriyMobile(mobileNumber)){ try { throw new Exception("不是完整的11位手机号或者正确的手机号前七位"); } catch (Exception e) { e.printStackTrace(); } } StringBuffer buffer = new StringBuffer(); String url = "http://www.ip138.com"; buffer.append(url); buffer.append(":8080");//端口 buffer.append("/"); buffer.append("search.asp?"); buffer.append("mobile=" + mobileNumber); buffer.append("&action=mobile"); String basePath = buffer.toString(); Document doc=Jsoup.parse(new URL(basePath), 3000); if(doc!=null){ //从class=tdc样式下面抓取 Elements tdcs = doc.getElementsByAttributeValue("class", "tdc"); for(Element td:tdcs){ //从class=tdc2样式下面抓取 Elements tdc2s=td.getElementsByAttributeValue("class","tdc2"); for(Element tdc:tdc2s){ //System.out.println(tdc); //<[^>]+>去掉html标签,去掉 html标签的空格 String mobileInfo=tdc.select("td").html().replaceAll("<[^>]+>", "").replaceAll(" ", "").replaceAll("-->", ""); System.out.println(mobileInfo); } } }else{ System.err.println("网络异常~~"); } } }
相关推荐
使用Jsoup解析html网页,包含jsoup.jar \ api.chm \ 代码
使用Jsoup库解析HTML、XML或URL链接中的DOM节点 Jsoup进行Html的Dom解析
jsoup解析html网页的api
支持Android4.0,真机与模拟器都测试通过,使用异步方式解析
使用httpClient采集数据,jsoup解析页面
使用jsoup对Html解析并生成excel
这是使用Jsoup解析HTML得到数据的一个小例子,具体的使用方法请参见我的博客:http://blog.csdn.net/ProgramChangesWorld/article/details/47134255
Jsoup解析html+xml
Jsoup HTML解析器For Java 在Java程序中使用JQuery操作DOM 模式识别的新技术 狂顶
使用android解析html文档,替换html中的内容
Jsoup库文件;Jsoup解析Java包
jsoup获取网站资源,按照自己的思路写自己的APP。
java解析xml,dom解析,jsoup解析,两种不同方式的解析,完整项目
用Jsoup解析html的所有jar包!
使用JSoup实现解析URL或HTML的内容
这个只是一个java源文件,用于学习Jsoup
用jsoup框架解析的一些东西里面解析了li Br a等等的一些标签还用到了线程刷新的一些简单的操作项目很简单
jxl报表,包括合并单元格,与jsoup(包括jsoup-1.7.2.jar包)解析高版本excel和jxl解析excel
利用jsoup工具解析html的demo
jsoup解析HTML,获取htmlUrl