public static String getHtmlByUrl(String url){
String html = null;
HttpClient httpClient = new DefaultHttpClient();//創建httpClient對象
HttpGet httpget = new HttpGet(url);//以get方式請求該URL
try {
HttpResponse responce = httpClient.execute(httpget);//得到responce對象
int resStatu = responce.getStatusLine().getStatusCode();//返回碼
if (resStatu==HttpStatus.SC_OK) {//200正常 其他就不對
//獲得相應實體
HttpEntity entity = responce.getEntity();
System.out.println(Integer.toString(resStatu));
if (entity!=null) {
html = EntityUtils.toString(entity);//獲得html源代碼
}
}
} catch (Exception e) {
System.out.println("訪問【"+url+"】出現異常!");
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
//System.out.println(html);
return html;
}
上述代碼只能抓靜態網頁數據
麻煩看下上面代碼怎麼改才能獲取動態網頁數據啊
用法是一樣的,只不過如果有些顯示數據是由js生成的,就沒辦法