从一个网页中获取所有的超链接
2008-10-31 13:40
393 查看
从一个网页中获取所有的超链接
/** @version 1.01 2004-06-04 @author Cay Horstmann */ import java.io.*; import java.net.*; import java.util.regex.*; /** This program displays all URLs in a web page by matching a regular expression that describes the <a href=...> HTML tag. Start the program as java HrefMatch URL */ |
public class HrefMatch { public static void main(String[] args) { try { // get URL string from command line or use default String urlString; if (args.length > 0) urlString = args[0]; else urlString = "http://java.sun.com"; // open reader for URL InputStreamReader in = new InputStreamReader(new URL(urlString).openStream()); // read contents into string buffer StringBuilder input = new StringBuilder(); int ch; while ((ch = in.read()) != -1) input.append((char) ch); // search for all occurrences of pattern String patternString = "<a//s+href//s*=//s*(/"[^/"]*/"|[^//s>])//s*>"; Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(input); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); String match = input.substring(start, end); System.out.println(match); } } catch (IOException e) { e.printStackTrace(); } catch (PatternSyntaxException e) { e.printStackTrace(); } } } 运行: java HrefMatch http://www.128kj.com <a href="linktype.jsp?type=1"> <a href="articleType_flex.html"> <a href="articleType_flex.html"> <a href="morephp.html"> <a href="morephp.html"> <a href="morejava.html"> <a href="morejava.html"> <a href="moreajax.html"> <a href="moreajax.html"> <a href="morejsp.html"> <a href="morejsp.html"> <a href="morejavascript.html"> <a href="morejavascript.html"> <a href="morexml.html"> <a href="morexml.html"> <a href="morecss.html"> <a href="morecss.html"> <a href="index7.html"> <a href="morePic.html"> <a href="moreFeibiao.html"> <a href="article/article45/logo/index0.html"> <a href="article/article46/bannerpic/index0.html"> <a href="moreTem.html"> <a href="moreTubiao.html"> <a href="moreJspSrc.html"> <a href="morePhpSrc.html"> <a href="article/article47/bg/index0.html"> <a href="morehtml.html"> <a href="morehtml.html"> <a href="morephotoshop.html"> <a href="morephotoshop.html"> <a href="http://www.miibeian.gov.cn"> |
相关文章推荐
- 从一个网页中获取所有的超链接
- 正则获取指定网页上的所有超链接
- 获取网页中的所有超链接
- python获取指定网页上所有超链接的方法
- c#通过webbrowser获取网页中的超链接地址
- PHP批量获取网页中所有固定种子链接的方法
- 【python】获取指定网页上的所有超级链接
- python获取网页中所有图片并筛选指定分辨率
- 网页爬虫的最简C/C++程序代码示例------先通过列表获取所有博文id, 然后遍历所有博文id
- 网页中如何获取客户端系统已安装的所有字体?
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
- 如何使用Hpple解析HTML,以及获取网站上的所有超链接
- C# 抓取并导出网页里面所有超链接方法
- 一行JavaScript代码获取页面中的所有超链接地址
- 获取网页中所有的email地址
- C#获取远程网页中的所有链接URL
- 网页中如何获取客户端系统已安装的所有字体?
- C#编程中(ASP.Net)获取当前网页路径的所有方法集合总结
- socket编程---获取网页上的超链接
- C#: 抓取网页类(获取网页中所有信息)