java提供图片链接,提取网页图片链接的JAVA程序

输入网页文件名,和资源列表文件名

输出资源列表文件供迅雷下载。

适用于批量下载图片。

由两个文件组成。

1// AnalizeIMG.java

2

3 // 主程序

4

5 import java.io.BufferedReader;

6import 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("\n");

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java WebMagic是一个开源的Java爬虫框架,可以用于爬取网页数据,包括图片。它提供了简单易用的API,可以方便地定义爬取规则和处理爬取结果。 要使用Java WebMagic进行图片爬取,首先需要添加WebMagic的依赖到你的项目中。然后,你可以按照以下步骤进行操作: 1. 创建一个Java类,作为你的爬虫程序的入口点。 2. 在该类中,使用WebMagic提供的注解和API定义你的爬取规则。你可以指定要爬取的网页URL、要提取图片链接等信息。 3. 实现一个自定义的处理器(Pipeline),用于处理爬取到的图片数据。你可以将图片保存到本地或者进行其他处理。 4. 创建一个爬虫对象,并设置好爬取规则和处理器。 5. 启动爬虫,开始爬取图片。 以下是一个简单的示例代码,演示了如何使用Java WebMagic进行图片爬取: ```java import us.codecraft.webmagic.Spider; import us.codecraft.webmagic.pipeline.FilePipeline; import us.codecraft.webmagic.processor.PageProcessor; public class ImageSpider { public static void main(String[] args) { // 创建一个PageProcessor对象,用于定义爬取规则 PageProcessor pageProcessor = new MyPageProcessor(); // 创建一个Pipeline对象,用于处理爬取结果 FilePipeline filePipeline = new FilePipeline("保存图片的目录"); // 创建一个Spider对象,并设置好PageProcessor和Pipeline Spider spider = Spider.create(pageProcessor) .addUrl("要爬取的网页URL") .addPipeline(filePipeline); // 启动爬虫 spider.run(); } // 自定义的PageProcessor类,用于定义爬取规则 static class MyPageProcessor implements PageProcessor { @Override public void process(Page page) { // 提取图片链接,并将其添加到爬取队列中 page.addTargetRequests(page.getHtml().$("img[src]").all()); // 获取图片链接,并保存到结果中 page.putField("image", page.getHtml().$("img[src]").all()); } @Override public Site getSite() { return Site.me(); } } } ``` 请注意,上述示例代码中的"要爬取的网页URL"和"保存图片的目录"需要根据实际情况进行替换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值