URL分別用三個List保存,
          一個是boring,這個list中的url最后來下載
          其他兩個是interesting和average
          當(dāng)搜索到url時檢查是否包含設(shè)定為boring的詞,并放入boring中

          用戶可設(shè)定“深度搜索”:每搜到一個url就放在list的最前面
          也可廣度

          有些網(wǎng)頁鏈接要特殊處理:

          url = textReplace("?", URLEncoder.encode("?"), url);
          url = textReplace("&", URLEncoder.encode("&"), url);
          private String textReplace(String find, String replace, String input)
          {
              int startPos = 0;
               while(true)
              {
                  int textPos = input.indexOf(find, startPos);
                  if(textPos < 0)
                  {
                      break;
                  }
                  input = input.substring(0, textPos) + replace + input.substring(textPos + find.length());
                  startPos = textPos + replace.length();
              }
              return input;
          }

          讀取資源代碼:

          BufferedInputStream remoteBIS = new BufferedInputStream(conn.getInputStream());
          ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
          byte[] buf = new byte[1024];
          int bytesRead = 0;
          while(bytesRead >= 0)
          {
              baos.write(buf, 0, bytesRead);
              bytesRead = remoteBIS.read(buf);
          }

          byte[] content = baos.toByteArray();


          建立多級目錄:

          File f = new File(fileName);
          f.getParentFile().mkdirs();
          FileOutputStream out = new FileOutputStream(fileName);
          out.write(content);
          out.flush();
          out.close();


          給一個變量寫doc:(在eclipse中,鼠標(biāo)置上會顯示)

          /**
          * Set of URLs downloaded or scheduled, so we don't download a
          * URL more than once.
          * Thread safety: To access the set, first synchronize on it.
          */
          private Set urlsDownloadedOrScheduled;


          這種log挺好:(apache log4j)

          private final static Category _logClass = Category.getInstance(TextSpider.class);

          /*
          顯示信息: 2005-05-01 11:40:44,250 [main] INFO? TextSpider.java:105 - Starting Spider...
          */
          _logClass.info("Starting Spider...");



          版權(quán)所有 羅明
          posted on 2006-02-16 14:10 羅明 閱讀(617) 評論(0)  編輯  收藏 所屬分類: Java
           
          主站蜘蛛池模板: 二连浩特市| 太康县| 内江市| 通城县| 阳朔县| 东乡县| 县级市| 德阳市| 吉林省| 南岸区| 吉木乃县| 睢宁县| 建阳市| 灵璧县| 长汀县| 海安县| 兰溪市| 潜山县| 定远县| 左云县| 柯坪县| 哈密市| 青龙| 余庆县| 松溪县| 山阴县| 福泉市| 如东县| 墨竹工卡县| 岑溪市| 四子王旗| 和硕县| 汉源县| 永康市| 普定县| 吉木萨尔县| 无为县| 含山县| 监利县| 三台县| 陆川县|