分享

如何用java实现抓取网页?

 真爱图书 2010-07-06
import java.net.*;
import java.io.*;

public class Catch1 {
     public void test(){
    StringBuffer document= new StringBuffer();
    try {
      URL url = new URL(http://www.sohu.com);
      URLConnection conn = url.openConnection();
      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line = null;
      while ((line = reader.readLine()) != null)
        document.append(line + "\n");
      reader.close();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    //pzy add
    String str = document.toString();
    String strDir = "E:\\text";
    String strFile = "test.html";
    File myFile = new File(strDir, strFile);

    try {
      myFile.createNewFile();
      BufferedWriter bw = new BufferedWriter(
                            new FileWriter(myFile.getPath(), true));
      bw.write(str);
      bw.flush();
      bw.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

     public static void main(String[] args){
      Catch1 catch2=new Catch1();
         catch2.test();
           }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多