Copy Code code as follows:
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 ();
Create a Get Method instance
GetMethod GetMethod = new GetMethod ("http://www.jb51.net");
Use the system-provided default recovery policy
Getmethod.getparams (). Setparameter (Httpmethodparams.retry_handler, New Defaulthttpmethodretryhandler ());
try{
Execute Get method
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 ();
Regular expressions
String regex = "<form name=\" compareform\ "[\\s\\s]+>[\\s\\s]+</form>.*<script.*>";
String regexa = "(?<=<li>) [\\s\\s]+? (?=</li>) ";
Pattern pattern = pattern.compile (regex);
Matcher m = pattern.matcher (HTML);
StringBuffer str = new StringBuffer ();
int i = 0;
while (M.find ()) {
Str.append (M.group ());
}
Pattern = Pattern.compile (regexa);
m = Pattern.matcher (str);
while (M.find ()) {
Attrs (M.group ());
i++;
}
System.out.println ("A total of" +i+ "piece of data!) ");
}catch (HttpException e) {
Todo:handle exception
System.out.println ("Please check your provided HTTP address!");
E.printstacktrace ();
}catch (IOException e) {
Todo:handle exception
SYSTEM.OUT.PRINTLN ("The line is wrong!");
E.printstacktrace ();
}finally{
Getmethod.releaseconnection ()//Free link
}
}
public static void Attrs (String str) {
Gets the regular expression of the URL
String Regexurl = "[a-z]+-[0-9]+\\.html";
Gets the regular expression of name
String regexname = "(? <=title=\") [[\\w-\\s][^x00-xff]]+ (? =\ ")";
Get a regular expression for a picture
String regexpicture = "Images.*\\.jpg";
Pattern Patternurl = Pattern.compile (Regexurl);
Pattern Patternname = Pattern.compile (regexname);
Pattern patternpicture = Pattern.compile (regexpicture);
Matcher Murl = Patternurl.matcher (str);
Matcher mname = Patternname.matcher (str);
Matcher mpicture = Patternpicture.matcher (str);
if (Mname.find ()) {
System.out.println ("Name:" +mname.group ());
}
if (Murl.find ()) {
System.out.println ("Link:" +murl.group ());
}
if (Mpicture.find ()) {
System.out.println ("Picture:" +mpicture.group ());
}
}
}