谁做过新闻采集器,能给我一个吗,我很着急用,就是输入一个网页的地址,把页面中的信息存到数据库中,
我的邮件[email protected]

解决方案 »

  1.   

    采集代码:(入库就自己写吧)
    <%@ page contentType="text/html; charset=GBK" %>
    <%@ page import = "java.io.*" %>
    <%@ page import = "java.net.*" %>
    <%@ page import = "java.util.*" %><%!
      private String Gid = "";
      private String Aid = "";
      static final private String CONTENT_TYPE = "text/html; charset=GBK";
      String filePath = "/home/portal/wwwroot/vod/index.html";
      private BufferedReader file;
      /**
       * Initialize global variables
       * @throws ServletException
       */
      public void init() throws ServletException {  }
      /**
       *
       * @param in
       * @param out
       * @throws Exception
       */
      public static void getURL(String in, OutputStream out) throws Exception {
          String protocol, local, host, filename;
          int port;      // use the URL class to parse the input
          URL url = new URL(in);
          protocol = url.getProtocol();
          if (! protocol.equals("http")) // only support http
              throw new IllegalArgumentException("Only support HTTP protocol.");      if ((host = url.getHost()) == null)
              throw new IllegalArgumentException("Invalid host specified.");      // if no port, use the default HTTP port
          if ((port = url.getPort()) == -1) port = 80;      // if no filename, request root directory index
          if((filename = url.getFile()) == "") filename = "/";      // open a network socket
          Socket socket = new Socket(host, port);      // get input and output streams from the socket
          InputStream from = socket.getInputStream();
          PrintWriter to = new PrintWriter(socket.getOutputStream());      to.print("GET " + filename + " HTTP/1.0\n");
          to.print("Accept: */*\n");
          to.print("Accept-Language: zh-cn\n");
          to.print("Accept-Encoding: gbk, deflate\n");
          to.print("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\n");
          to.print("Host: " + url.getHost() + "\n");
          to.print("Connection: Keep-Alive\n\n");
          to.flush();      // read server response and write it to the specified output stream
          byte[] buf = new byte[4096];
          int bytes_read;      while((bytes_read = from.read(buf)) != -1)
              out.write(buf, 0, bytes_read);      // close the server and file streams
          socket.close();
          out.close();
      }  public static String getList(String html) {
          int StartPos = -1;
          int EndPos = -1;
          int Length = -1;
          StartPos = html.indexOf("<html>",1);      EndPos = html.indexOf("</html>",1);
          return html.substring(StartPos,EndPos);
      }
      /**
       *
       * @param out
       * @param html
       */
      public void dispIndex(JspWriter out,String html) throws IOException{
          out.println(html);
      }
    %><%
          ByteArrayOutputStream outStream
                                    = new ByteArrayOutputStream();
          String html = "";      Gid = request.getParameter("Gid");
          Aid = request.getParameter("Aid");      if(Gid == null && Aid == null) {
              
              try {
                  this.getURL("http://192.168.1.114/vod/indexO.jsp", outStream);
                  html = outStream.toString();//采集到的信息
                  html = getList(html);
              } catch (Exception e) {
                  System.err.println("error:" + e);
              }
              try { PrintWriter pw = new PrintWriter(new FileOutputStream(filePath)); pw.println(html); pw.close(); } catch(IOException e) { System.out.println("写入文件错误"+e.getMessage());
    } response.sendRedirect("http://221.208.250.140/vod/convertToStatic11.jsp");
    }
    %>