Java实现一个简单的网络爬虫

时间:2022-07-24 22:58:23

Java实现一个简单的网络爬虫

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL; public class BasicSpaider { public static void main(String[] args) {
URL url = null;
InputStream is = null;
BufferedReader br = null;
FileWriter fw = null;
BufferedWriter bw = null;
String temp = null;
StringBuilder sb = new StringBuilder();
try {
fw = new FileWriter("C:/Users/Xiaofei/Desktop/163首页.txt",true);
bw = new BufferedWriter(fw);
url = new URL("http://www.163.com");
is = url.openStream();
br = new BufferedReader(new InputStreamReader(is,"GBK"));
while((temp = br.readLine())!=null){
bw.write(temp);
sb.append(temp);
}
System.out.println(sb+"/n完成");
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally{
try {
bw.close();
fw.close();
br.close();
is.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} } }