加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式知识详解之实战 读取网页中超链接 (java版示例)

发布时间:2020-12-14 04:25:43 所属栏目:百科 来源:网络整理
导读:正则表达式知识详解系列,通过代码示例来说明正则表达式知识 源代码下载地址: http://download.csdn.net/detail/gnail_oug/9504094 示例功能: 1、根据一个url,获取页面里的所有的超链接 步骤一、根据url读取页面内容 /** * 根据url读取网页内容 * @date 2

正则表达式知识详解系列,通过代码示例来说明正则表达式知识

源代码下载地址:http://download.csdn.net/detail/gnail_oug/9504094


示例功能:

1、根据一个url,获取页面里的所有的超链接


步骤一、根据url读取页面内容
	/**
	 * 根据url读取网页内容
	 * @date 2016-04-27 10:34:13
	 * @author sgl
	 * @param urlStr
	 * @return
	 */
	public static String readHtml(String urlStr){
		StringBuffer sb=new StringBuffer("");
		BufferedReader br=null;
		try {
			URL url=new URL(urlStr);
			HttpURLConnection conn=(HttpURLConnection)url.openConnection();
			InputStream in=conn.getInputStream();
			br=new BufferedReader(new InputStreamReader(in));
			String line=null;
			while((line=br.readLine())!=null){
				sb.append(line);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return sb.toString();
	}

步骤二、从页面内容中找超链接
	/**
	 * 从字符串中找出超链接
	 * @date 2016-04-27 10:35:27
	 * @author sgl
	 * @param str
	 * @return
	 */
	public static List<String>findLink(String str){
		Pattern p=Pattern.compile("<[Aa]s+(.*?s+)*?hrefs*=s*(["']).+?2s*(s+.*?s*)*?>.+?</[Aa]>");
		Matcher m=p.matcher(str);
		List<String>list=new ArrayList<String>();
		while(m.find()){
			list.add(m.group());
		}
		return list;
	}

步骤三、获取超链接

	public static void main(String[] args) {
		String htmlTxt=Demo03.readHtml("http://www.csdn.net/");
		List<String>list=Demo03.findLink(htmlTxt);
		for(String str:list){
			System.out.println(str);
		}
		System.out.println(list.size());
	}

运行结果:(中间部分省略了)

<a href="https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn">登录</a>
<a href="http://passport.csdn.net/account/mobileregister?action=mobileRegister">注册</a>
<a href="https://passport.csdn.net/help/faq">帮助</a>
...
<a href="http://www.csdn.net/company/icp.html">电信业务审批[2007]字第380号</a>
<a href="http://www.csdn.net/company/pifu.html">电信与信息服务业务经营许可证070598号</a>
<a href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010" target="_blank"><img src="http://c.csdnimg.cn/www/images/gongshang_logos.gif" alt="GongshangLogo" alt="' title="" /></a>
394

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读