Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 452|回复: 0

[网络编程学习]提取网页图片链接的Java程序

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-28 23:58:24 | 显示全部楼层 |阅读模式
    输入网页文件名,和资源列表文件名
    输出资源列表文件供迅雷下载。
    适用于批量下载图片。
    由两个文件组成。
    没有提供网页下载功能,因为我没有时间写,相关的代码以后再贴。
       
         1
        //
        AnalizeIMG.java

         2
       

         3
         
        //
        主程序
       

         4
         
       

         5
         
        import
         java.io.BufferedReader;

         6
        import
         java.io.File;

         7
         
        import
         java.io.FileReader;

         8
         
        import
         java.io.FileWriter;

         9
         
        import
         java.io.IOException;

        10
         

        11
         

        12
         
        public
          
        class
         AnalizeIMG  
       
        {
    13
    14 public   void  p(String s)
    15 {
    16    System.out.println(s);
    17 }
    18
    19 public   void  analizeFile(String infile,String outfile)  throws  Exception
    20 {
    21    File file  =   new  File(infile);
    22      if  (file  ==   null   ||   ! file.exists())   {
    23     p( "File"   +  infile  +   "not exits !" );
    24    }
    25
    26      if  ( ! file.canRead())   {
    27     p( "File"   +  infile  +" can"t read !" );
    28
    29    }
    30   
    31    String strLine  =   null ;
    32    FileReader frd  =   new  FileReader(infile);
    33    BufferedReader bufferedReader  =   new  BufferedReader(frd);
    34      try    {
    35     AnalizeWebParse parse  =   new  AnalizeWebParse();
    36     String s  =  parse.parse(bufferedReader);
    37   
    38     createFile(outfile,s);
    39   
    40     }   catch  (Exception ex)   {
    41      throw  ex;
    42     }   finally    {
    43     frd.close();
    44     bufferedReader.close();
    45    }
    46 }
    47
    48 private   void  createFile(String filename, String content)   {
    49    FileWriter f  =   null ;
    50      try    {
    51     f  =   new  FileWriter(filename);
    52       if  (f  ==   null   ||  content  ==   null )   {
    53       return ;
    54     }
    55
    56     f.write(content);
    57     f.flush();
    58     f.close();
    59
    60     }   catch  (Exception e)   {
    61
    62     }   finally    {
    63       if  (f!=   null )   {
    64        try {
    65       f.close();
    66       }   catch  (Exception e)   {
    67
    68      }
    69     }
    70    }
    71 }
    72
    73public   static   void  main(String arg[])
    74 {
    75    AnalizeIMG ana  =   new  AnalizeIMG();
    76      try {
    77     ana.analizeFile("E:\1.txt" , "E:\out.lst");
    78     } catch  (Exception ex)   {
    79     ex.printStackTrace();
    80    }
    81 }
    82 }
       

        83
         

        84
         
       
    第二个文件时解析文件
       
         1
        //
        AnalizeWebParse.java

         2
         

         3
         
        //
        网页分析代码,需要用户根据自己需要做适当修改
       

         4
         
       

         5
         
        import
         java.io.BufferedReader;

         6
         
        import
         java.io.StringReader;

         7
         
        import
         java.util.regex.Pattern;

         8
         

         9
         
        import
         javax.swing.text.MutableAttributeSet;

        10
         
        import
         javax.swing.text.HTML.HTML;

        11
         
        import
         javax.swing.text.html.HTMLEditorKit.ParserCallback;

        12
         
        import
         javax.swing.text.html.parser.ParserDelegator;

        13

         
        14
         
        public
          
        class
         AnalizeWebParse  
        extends
         ParserCallback  
       
        {
    15
    16 StringBuffer sb  =   new  StringBuffer();
    17
    18 boolean  start =false ;
    19 boolean  finished =false ;
    20
    21 public   void  p(String s)
    22 {
    23    System.out.println(s);
    24 }
    25
    26 public   void  handleStartTag(HTML.Tag tag, MutableAttributeSet attribs,
    27       int  pos)   {
    28   
    29     if (finished  ==   true )
    30       {
    31      return ;
    32    }
    33   
    34      if  (start  ==   false )   {
    35       if  (tag  ==  HTML.Tag.DIV)   {
    36     String cla  =  (String) attribs
    37        .getAttribute(HTML.Attribute.CLASS);
    38        if  (cla  ==   null )   {
    39       return ;
    40      }
    41
    42        if  (cla.indexOf("body")!=   - 1 )   {
    43        //  Start
    44       start  =   true ;
    45      }
    46     }
    47    }
    48 }
    49
    50 public   void  handleEndTag(HTML.Tag tag,  int  pos)   {
    51      if  (tag  ==  HTML.Tag.DIV  &&  start  ==   true   &&  finished  ==   false )   {
    52     finished  =   true ;
    53    }
    54 }
    55
    56 public   void  handleText( char [] text,  int  pos)   {
    57
    58 }
    59
    60 public   void  handleSimpleTag(HTML.Tag t, MutableAttributeSet a,  int  pos)   {
    61      if  (t  ==  HTML.Tag.IMG)   {
    62      //  get a src
    63     String src  =  (String) a.getAttribute(HTML.Attribute.SRC);
    64       if  (src  ==   null )   {
    65       return ;
    66     }
    67   
    68       if  (Pattern.matches("^(http://.+)" , src))   {
    69      sb.append(src).append("
    ");
    70     }
    71    }
    72 }
    73
    74 public  String parse(BufferedReader file)  throws  Exception   {
    75     if (file == null )
    76       {
    77      return   null ;
    78    }
    79   
    80    ParserDelegator pd  =   new  ParserDelegator();
    81      try    {
    82     pd.parse(file,  this ,  true );
    83     }   catch(Exception e)   {
    84      throw  e;
    85    }
    86   
    87     return  sb.toString();
    88 }
    89 }
       

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/28/235823484.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 01:17 , Processed in 0.358806 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表