JAVA使用爬蟲抓取網(wǎng)站網(wǎng)頁內(nèi)容的方法
本文實例講述了JAVA使用爬蟲抓取網(wǎng)站網(wǎng)頁內(nèi)容的方法。分享給大家供大家參考。具體如下:
最近在用JAVA研究下爬網(wǎng)技術(shù),呵呵,入了個門,把自己的心得和大家分享下
以下提供二種方法,一種是用apache提供的包.另一種是用JAVA自帶的.
代碼如下:
// 第一種方法 //這種方法是用apache提供的包,簡單方便 //但是要用到以下包:commons-codec-1.4.jar // commons-httpclient-3.1.jar // commons-logging-1.0.4.jar public static String createhttpClient(String url, String param) { HttpClient client = new HttpClient(); String response = null; String keyword = null; PostMethod postMethod = new PostMethod(url); // try { // if (param != null) // keyword = new String(param.getBytes("gb2312"), "ISO-8859-1"); // } catch (UnsupportedEncodingException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } // NameValuePair[] data = { new NameValuePair("keyword", keyword) }; // // 將表單的值放入postMethod中 // postMethod.setRequestBody(data); // 以上部分是帶參數(shù)抓取,我自己把它注銷了.大家可以把注銷消掉研究下 try { int statusCode = client.executeMethod(postMethod); response = new String(postMethod.getResponseBodyAsString() .getBytes("ISO-8859-1"), "gb2312"); //這里要注意下 gb2312要和你抓取網(wǎng)頁的編碼要一樣 String p = response.replaceAll("http://&[a-zA-Z]{1,10};", "") .replaceAll("<[^>]*>", "");//去掉網(wǎng)頁中帶有html語言的標簽 System.out.println(p); } catch (Exception e) { e.printStackTrace(); } return response; } // 第二種方法 // 這種方法是JAVA自帶的URL來抓取網(wǎng)站內(nèi)容 public String getPageContent(String strUrl, String strPostRequest, int maxLength) { // 讀取結(jié)果網(wǎng)頁 StringBuffer buffer = new StringBuffer(); System.setProperty("sun.net.client.defaultConnectTimeout", "5000"); System.setProperty("sun.net.client.defaultReadTimeout", "5000"); try { URL newUrl = new URL(strUrl); HttpURLConnection hConnect = (HttpURLConnection) newUrl .openConnection(); // POST方式的額外數(shù)據(jù) if (strPostRequest.length() > 0) { hConnect.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(hConnect .getOutputStream()); out.write(strPostRequest); out.flush(); out.close(); } // 讀取內(nèi)容 BufferedReader rd = new BufferedReader(new InputStreamReader( hConnect.getInputStream())); int ch; for (int length = 0; (ch = rd.read()) > -1 && (maxLength <= 0 || length < maxLength); length++) buffer.append((char) ch); String s = buffer.toString(); s.replaceAll("http://&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", ""); System.out.println(s); rd.close(); hConnect.disconnect(); return buffer.toString().trim(); } catch (Exception e) { // return "錯誤:讀取網(wǎng)頁失??!"; // return null; } }
然后寫個測試類:
public static void main(String[] args) { String url = "http://www.dbjr.com.cn"; String keyword = "腳本之家"; createhttpClient p = new createhttpClient(); String response = p.createhttpClient(url, keyword); // 第一種方法 // p.getPageContent(url, "post", 100500);//第二種方法 }
呵呵,看看控制臺吧,是不是把網(wǎng)頁的內(nèi)容獲取了
希望本文所述對大家的java程序設(shè)計有所幫助。
相關(guān)文章
Java實現(xiàn)byte[]轉(zhuǎn)List的示例代碼
byte,即字節(jié),由8位的二進制組成。在Java中,byte類型的數(shù)據(jù)是8位帶符號的二進制數(shù)。List?是一個接口,它繼承于Collection的接口。它代表著有序的隊列。本文將介紹如何通過java實現(xiàn)byte[]轉(zhuǎn)List,需要的可以參考一下2022-01-01java編程實現(xiàn)根據(jù)EXCEL列名求其索引的方法
這篇文章主要介紹了java編程實現(xiàn)根據(jù)EXCEL列名求其索引的方法,涉及Java元素遍歷與數(shù)學運算的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11java批量采集豌豆莢網(wǎng)站Android應(yīng)用圖標和包名
這篇文章主要介紹了java批量采集豌豆莢網(wǎng)站Android應(yīng)用圖標和包名,主要用在做主題時替換這些常見應(yīng)用的圖片,需要的朋友可以參考下2014-06-06