爬蟲相關(1)--- 如何使用java來獲取HTML源碼

不需要添加任何jar文件就可以通過網站連接來得到網站的html源代碼


		URL url = new URL("http://www.baidu.com/");

		HttpURLConnection conn = (HttpURLConnection) url.openConnection();

		InputStream inputStream = conn.getInputStream(); // 通過輸入流獲得網站數據

		byte[] getData = readInputStream(inputStream); // 獲得網站的二進制數據

其中readInputStream(InputStream)方法非常重要,可以把InputStream對象中的流讀出來,存儲爲Byte數組的形式,代碼如下:

//把InputStream裏面的數據轉化爲byte數組,返回。該方法很重要!
	public static byte[] readInputStream(InputStream inputStream)
			throws IOException {
		byte[] buffer = new byte[1024];
		int len = 0;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while ((len = inputStream.read(buffer)) != -1) {
			bos.write(buffer, 0, len) ;
		}

		bos.close();
		return bos.toByteArray();
	}

最後一步把byte數組處理一下,編程字符串形式,就可以隨便處理了。將byte[] ----> String 方法如下:

public String parseByteArrayToString(byte[] buf){

return new String(buf) ;  //構造方法也可以是String(byte[], Encoding), 這樣可以對新構造的字符串進行編碼方面的控制

}



程序源碼以及測試如下:(把原文件讀出來轉爲字符串存儲在txt文件)

package FromBottom;


import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;


public class HtmlRequest {


public static void main(String[] args) throws Exception {
URL url = new URL("http://www.baidu.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream inputStream = conn.getInputStream(); // 通過輸入流獲得網站數據
byte[] getData = readInputStream(inputStream); // 獲得網站的二進制數據


newToFile(getData) ;
//toFile_good("log_tofilegood.txt", data, "UTF-8") ;
//toFile2(data) ;
}


//把InputStream裏面的數據轉化爲byte數組,返回。該方法很重要!
public static byte[] readInputStream(InputStream inputStream)
throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len) ;
}


bos.close();
return bos.toByteArray();
}


// 將String寫入文件,這個方法可以運行沒問題。
public static void toFile_good(String filename, String string,
String charset) throws IOException {
Charset cs = Charset.forName(charset);
CharsetEncoder coder = cs.newEncoder();
FileOutputStream stream = new FileOutputStream(filename, true);
OutputStreamWriter writer = new OutputStreamWriter(stream, coder);
writer.write(string, 0, string.length());
writer.close();
}

//使用FileWriter將String寫入文件,運行沒問題
public static void toFile2(String content) throws Exception{
FileWriter fw = new FileWriter(new File("log.txt")) ;
fw.write(content) ;
fw.close() ;
}

//這個方法可以把字符串完整的輸出到txt文件,參數爲byte數組,運行沒問題
public static void newToFile(byte[] buf) throws IOException{
FileWriter fw = new FileWriter("newToFile.txt") ;
BufferedWriter bw = new BufferedWriter(fw) ;
String str = new String(buf, "UTF-8") ;
System.out.println(str) ;
bw.write(str) ;
bw.flush() ;
bw.close() ; 
fw.close() ;
}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章