コードコピーは次のとおりです。
パッケージ作業。
java.io.bufferedreaderをインポートします。
java.io.ioexceptionをインポートします。
java.io.inputStreamをインポートします。
java.io.inputStreamReaderをインポートします。
java.util.regex.matcherをインポートします。
java.util.regex.patternをインポートします。
org.apache.commons.httpclient.defaulthttpmethodretryhandlerをインポートします。
Import org.apache.commons.httpclient.httpclient;
Import org.apache.commons.httpclient.httpexception;
org.apache.commons.httpclient.httpstatusをインポートします。
org.apache.commons.httpclient.methods.getMethod;
org.apache.commons.httpclient.params.httpmethodparamsをインポートします。
パブリッククラスchuanboyi {
public static void main(string [] args){
// TODO自動生成方法スタブ
stringbuffer html = new StringBuffer();
httpclient httppclient = new httpclient();
// getメソッドインスタンスを作成します
getMethod getMethod = new getMethod( "// www.vevb.com");
//システムが提供するデフォルトの回復ポリシーを使用します
getMethod.getParams()。setParameter(httpmethodparams.retry_handler、new defaulthtpmethodretryhandler());
試す{
// getメソッドを実行します
int statusCode = httpclient.executeMethod(getMethod);
if(statuscode!= httpstatus.sc_ok){
system.out.println( "メソッドは間違っています" + 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 = "<form name =/" Compartform/"[// s // s]+> [// s // s]+</form>。*<script。*>";
string regexa = "(?<= <li>)[// s // s]+?(?= </li>)";
パターンパターン= 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( "合計"+i+"data!");
} catch(httpexception e){
// TODO:例外を処理します
System.out.println( "提供されているHTTPアドレスを確認してください!");
e.printstacktrace();
} catch(ioException e){
// TODO:例外を処理します
system.out.println( "ラインが間違っています!");
e.printstacktrace();
} ついに{
getMethod.releaseConnection(); //リンクをリリースします
}
}
public static void attrs(string str){
// URLの正規表現を取得します
string regexurl = "[az]+ - [0-9]+//。html";
//名前の正規表現を取得します
string regexname = "(?<= title =/")[[// w - // s] [^x00-xff]+(?=/")";
//画像の正規表現を取得します
string regexpicture = "images。*//。jpg";
pattern patternurl = pattern.compile(regexurl);
パターンパターン名= pattern.compile(regexname);
パターンパターンPicture = 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());
}
}
}