HttpClient與HtmlParse完美融合簡單例項
收索HttpClient下載
收索HtmlParse下載
簡單例項程式碼:
package com.fldyown.advertisement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
public class Advertisement {
/**
* @Enclosing_Method : main
* @Written by : 強
* @Creation Date : 2011-5-17 下午09:01:41
* @version : v1.00
* @Description :
* @param args
**/
public static void main(String[] args) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(
"http://www.baidu.com/s?wd=%B7%E3%C1%F8%B5%FB%D4%CF");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
Parser parser = new Parser(EntityUtils.toString(entity));
NodeList nodeList = parser.parse(null);
NodeFilter filter = new TagNameFilter("a");
NodeList list = nodeList.extractAllNodesThatMatch(filter, true);
for (int i = 0; i < list.size(); i++) {
LinkTag tag = (LinkTag) list.elementAt(i);
System.out.println(tag.getAttribute("href") + "\n");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
這個程式很有用,很多時候我們可能需要過濾網頁中的一些我們關心的資料,如圖片連結,等等,就可以用到!