Java实现一个简单的网络爬虫
Java实现一个简单的网络爬虫
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.FileWriter; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 import java.net.MalformedURLException; 8 import java.net.URL; 9 10 11 public class BasicSpaider { 12 13 public static void main(String[] args) { 14 URL url = null; 15 InputStream is = null; 16 BufferedReader br = null; 17 FileWriter fw = null; 18 BufferedWriter bw = null; 19 String temp = null; 20 StringBuilder sb = new StringBuilder(); 21 try { 22 fw = new FileWriter("C:/Users/Xiaofei/Desktop/163首页.txt",true); 23 bw = new BufferedWriter(fw); 24 url = new URL("http://www.163.com"); 25 is = url.openStream(); 26 br = new BufferedReader(new InputStreamReader(is,"GBK")); 27 while((temp = br.readLine())!=null){ 28 bw.write(temp); 29 sb.append(temp); 30 } 31 System.out.println(sb+"/n完成"); 32 } catch (MalformedURLException e) { 33 // TODO 自动生成的 catch 块 34 e.printStackTrace(); 35 } catch (IOException e) { 36 // TODO 自动生成的 catch 块 37 e.printStackTrace(); 38 }finally{ 39 try { 40 bw.close(); 41 fw.close(); 42 br.close(); 43 is.close(); 44 } catch (IOException e) { 45 // TODO 自动生成的 catch 块 46 e.printStackTrace(); 47 } 48 } 49 50 } 51 52 }