java正则表达式解析html示例分享
本文导语: 代码如下:package work; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.regex.Matcher;import java.util.regex.Pattern; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;import org.apache.comm...
package work;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class chuanboyi {
public static void main(String[] args){
// TODO Auto-generated method stub
StringBuffer html = new StringBuffer();
HttpClient httpclient = new HttpClient();
//创建GET方法实例
GetMethod getMethod = new GetMethod("http://www.");
//使用系统提供的默认恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try{
//执行GET方法
int statusCode = httpclient.executeMethod(getMethod);
if(statusCode != HttpStatus.SC_OK){
System.out.println("Method is wrong " + getMethod.getStatusLine());
}
InputStream responseBody = getMethod.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody,"utf-8"));
String line = reader.readLine();
while(line != null){
html.append(line).append("n");
line = reader.readLine();
}
reader.close();
//正则表达式
String regex = "[\s\S]+.*";
String regexa ="(?