从网页源文件中得到链接

 
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4. class ScanPage{
  5.     private static String strPage;
  6.     private String strUrl;
  7.     private String fileName;
  8.     public void setURL(String strUrl){
  9.         this.strUrl=strUrl;
  10.     }
  11.     //从地址中得到文件名
  12.     public void setFileName(){
  13.         int i;
  14.         char ch;
  15.         i=strUrl.length();
  16.         ch=strUrl.charAt(--i);
  17.         while(ch!='/' && ch>0)
  18.             ch=strUrl.charAt(--i);
  19.         fileName=strUrl.substring(i);
  20.     }
  21.     //下载网页
  22.     public void downFile()throws IOException{ 
  23.         URL url =new URL(strUrl);
  24.         InputStream is =url.openStream();
  25.         OutputStream os =new FileOutputStream(fileName);
  26.         byte[] buffer =new byte[512];
  27.         int len;
  28.         while((len =is.read(buffer))!=-1
  29.             os.write(buffer,0,len);
  30.         is.close();
  31.         os.close();
  32.     }
  33.     //读文件
  34.     public void readFile() throws IOException {
  35.         StringBuffer sb = new StringBuffer();
  36.         BufferedReader in =new BufferedReader(new FileReader(fileName));
  37.         String s;
  38.         while((s = in.readLine()) != null) {
  39.             sb.append(s);
  40.             sb.append("/n");
  41.         }
  42.         in.close();
  43.         strPage=sb.toString();
  44.     }
  45.     public String getTitle(){
  46.         
  47.         return "";
  48.     }
  49.     //扫描标签,得到资源
  50.     public void scanLabel(ArrayList al,String strLabel,String strType){
  51.         int idx;   //返回下标
  52.         String strTmp;
  53.         strPage=strPage.toLowerCase();  //转换为小写,以便后面比较
  54.         idx = strPage.indexOf("<body");
  55.         while(idx!=-1){
  56.             idx=strPage.indexOf(strLabel,idx);
  57.             if (idx==-1)
  58.                 break;
  59.             else{
  60.                 int i=0;
  61.                 idx=idx+strLabel.length();
  62.                 for(;strPage.charAt(idx+i)!='>' && strPage.charAt(idx+i)!=32;++i);
  63.                 strTmp=strPage.substring(idx,idx+i);
  64.                 idx=idx+i;
  65.                 //去掉首尾引号
  66.                 if (strTmp.charAt(0)=='/"')
  67.                     strTmp=strTmp.substring(1);
  68.                 if (strTmp.charAt(strTmp.length()-1)=='/"')
  69.                     strTmp=strTmp.substring(0,strTmp.length()-1);
  70.             //判断是否是用户需要的类型
  71.                 if (strType.equals("*"))
  72.                     al.add(strTmp);
  73.                 else{
  74.                     String right;
  75.                     if (strTmp.length()>=strType.length()){
  76.                         right=strTmp.substring(strTmp.length()-strType.length());
  77.                         right=right.toLowerCase();
  78.                         if (right.equals(strType))
  79.                             al.add(strTmp);
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }
  86. class ScanApp{
  87.     public static void main(String[] args){
  88.         ArrayList al=new ArrayList();
  89.         ScanPage sp=new ScanPage();
  90.         sp.setURL(args[0]);
  91.         sp.setFileName();
  92.         try{
  93.             sp.downFile();
  94.             sp.readFile();
  95.         }catch(IOException ie){System.out.println("文件操作出错");};
  96.         sp.scanLabel(al,"<a href=",".html");
  97.         for(int i=0;i<al.size();i++){
  98.             System.out.println(al.get(i));
  99.         }
  100.     }
  101. }
  102. E:/javawork>java ScanApp http://www.17kyk.com/Html/Book/16/2431/list.html
  103. list.html
  104. 429400.html
  105. 429401.html
  106. 439789.html
  107. 429403.html
  108. 429404.html
  109. 429405.html
  110. 429406.html
  111. 429407.html
  112. 429408.html
  113. 429409.html
  114. 429411.html
  115. 429412.html
  116. 说明:
  117. scanLabel针对<a href和<img src这2个标签进行扫描.
  118. 得到所有链接存储在ArrayList中
  119. scanLabel(al,"<a href=","*");
  120. scanLabel(al,"<img src=","*");
  121. 得到特定的链接
  122. scanLabel(al,"<a href=",".html");
  123. scanLabel(al,"<a href=",".asp");
  124. scanLabel(al,"<img src=",".gif");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值