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
           
          主站蜘蛛池模板: 金坛市| 马鞍山市| 鄂温| 远安县| 班戈县| 台中市| 开江县| 九台市| 鄢陵县| 惠安县| 吉首市| 大姚县| 罗甸县| 台湾省| 五家渠市| 东山县| 于都县| 扬州市| 河北省| 万山特区| 安塞县| 新乡市| 兴化市| 广南县| 定远县| 南华县| 松江区| 独山县| 遂宁市| 海淀区| 南通市| 兰州市| 冀州市| 洪湖市| 桓仁| 晋州市| 武川县| 甘南县| 肇东市| 沁水县| 灯塔市|