利用JSP获取网页源文件并抓取其中的链接地址
利用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("https://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>");
}
%>