利用java的net包獲取網頁源文件,使用正則表達式抓取當中的鏈接地址,因正則表達式學藝不精,下面這個例子中並不能抓取所有情況下的href屬性中的鏈接地址
test.jsp(SUN企業級應用的首選)
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.regex.*" errorPage="" %>
<%
String sCurrentLine;
String sTotalString;
sCurrentLine="";
sTotalString="";
java.io.InputStream l_urlStream;
java.net.URL l_url = new java.net.URL("http://www.5ja.net");
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
l_connection.connect();
l_urlStream = l_connection.getInputStream();
java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream));
while ((sCurrentLine = l_reader.readLine()) != null)
{
sTotalString+=sCurrentLine;
}
//String regEx = "href=([^"]*)>";
String regEx ="href="([^"]*)"";//找出href="****"的鏈接
Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(sTotalString);
int j=0;
while(m.find()){
j++;
out.println("m.group(" + j + "): " + m.group(0)+"<br>");
}
regEx ="href=([^]*)";//找出href=****的鏈接
p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);//Pattern.CASE_INSENSITIVE查找忽略大小寫
m = p.matcher(sTotalString);
while(m.find()){
j++;
out.println("m.group(" + j + "): " + m.group(0)+"<br>");
}
%>