提取网页的图片链接的Java程序

输入网页文件名,和资源列表文件名
输出资源列表文件供迅雷下载。
适用于批量下载图片。
由两个文件组成。
没有提供网页下载功能,因为我没有时间写,相关的代码以后再贴。

 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
14public void p(String s)
15{
16   System.out.println(s);
17}

18
19public 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
48private 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
16StringBuffer sb = new StringBuffer();
17
18boolean start = false;
19boolean finished = false;
20
21public void p(String s)
22{
23   System.out.println(s);
24}

25
26public 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
50public void handleEndTag(HTML.Tag tag, int pos) {
51   if (tag == HTML.Tag.DIV && start == true && finished == false{
52    finished = true
53   }

54}

55
56public void handleText(char[] text, int pos) {
57
58}

59
60public 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("\n");
70    }

71   }

72}

73
74public 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, thistrue);
83   }
 catch (Exception e) {
84    throw e;
85   }

86  
87   return sb.toString();
88}

89}

90

转载于:https://www.cnblogs.com/xiaoyz/archive/2008/06/09/1216271.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值