1. java简单web爬虫(网页图片)
    效果,执行main()方法后图片就下载道C盘的res文件夹中。没有的话创建一个文件夹
    代码里的常量根据自己的需求修改,代码附到下面。

 

  1. package com.sinitek.sirm.common.utils;
  2.  
  3. import java.io.*;
  4. import java.net.URL;
  5. import java.net.URLConnection;
  6. import java.util.*;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. /**
  11. * java简单web爬虫(网页图片)
  12. */
  13. public class Main {
  14.  
  15. // 地址
  16. private static final String URL = "http://www.xxx";
  17. // 获取img标签正则
  18. private static final String IMGURL_REG = "<img.*src=(.*?)[^>]*?>";
  19. // 获取src路径的正则
  20. private static final String IMGSRC_REG = "src\\s*=\\s*\"?(.*?)(\"|>|\\s+)";
  21. //图片原始路径(如果src里的路径正确则不用)
  22. private static final String IMG_LUJING = "http://xxx/";
  23. //下载路径
  24. private static final String LUJING = "C:/res/";
  25.  
  26. public static void main(String[] args) {
  27. try {
  28. Main cm=new Main();
  29. //获得html文本内容
  30. String HTML = cm.getHtml(URL);
  31. //获取图片标签
  32. List<String> imgUrl = cm.getImageUrl(HTML);
  33. //获取图片src地址
  34. List<String> imgSrc = cm.getImageSrc(imgUrl);
  35. //下载图片
  36. cm.Download(imgSrc);
  37.  
  38. }catch (Exception e){
  39. System.out.println("发生错误");
  40. }
  41.  
  42. }
  43.  
  44. //获取HTML内容
  45. private String getHtml(String url)throws Exception{
  46. URL url1=new URL(url);
  47. URLConnection connection=url1.openConnection();
  48. InputStream in=connection.getInputStream();
  49. InputStreamReader isr=new InputStreamReader(in);
  50. BufferedReader br=new BufferedReader(isr);
  51.  
  52. String line;
  53. StringBuffer sb=new StringBuffer();
  54. while((line=br.readLine())!=null){
  55. sb.append(line,0,line.length());
  56. sb.append('\n');
  57. }
  58. br.close();
  59. isr.close();
  60. in.close();
  61. return sb.toString();
  62. }
  63.  
  64. //获取ImageUrl地址
  65. private List<String> getImageUrl(String html){
  66. Matcher matcher=Pattern.compile(IMGURL_REG).matcher(html);
  67. List<String>listimgurl=new ArrayList<String>();
  68. while (matcher.find()){
  69. listimgurl.add(matcher.group());
  70. }
  71. return listimgurl;
  72. }
  73.  
  74. //获取ImageSrc地址
  75. private List<String> getImageSrc(List<String> listimageurl){
  76. List<String> listImageSrc=new ArrayList<String>();
  77. for (String image:listimageurl){
  78. // 匹配<img>中的src数据
  79. Matcher m = Pattern.compile(IMGSRC_REG).matcher(image);
  80. while (m.find()) {
  81. String a = m.group(1);//获取图片路径
  82. a = IMG_LUJING+a;//数据拼接
  83. listImageSrc.add(a);
  84. }
  85. }
  86. return listImageSrc;
  87. }
  88.  
  89. //下载图片
  90. private void Download(List<String> listImgSrc) {
  91. try {
  92. //开始时间
  93. Date begindate = new Date();
  94. for (String url : listImgSrc) {
  95. //开始时间
  96. Date begindate2 = new Date();
  97. String imageName = url.substring(url.lastIndexOf("/") + 1, url.length());
  98. URL uri = new URL(url);
  99. InputStream in = uri.openStream();
  100. FileOutputStream fo = new FileOutputStream(new File(LUJING+imageName));//路径
  101. byte[] buf = new byte[1024];
  102. int length = 0;
  103. System.out.println("开始下载:" + url);
  104. while ((length = in.read(buf, 0, buf.length)) != -1) {
  105. fo.write(buf, 0, length);
  106. }
  107. in.close();
  108. fo.close();
  109. System.out.println(imageName + "下载完成");
  110. //结束时间
  111. Date overdate2 = new Date();
  112. double time = overdate2.getTime() - begindate2.getTime();
  113. System.out.println("耗时:" + time / 1000 + "s");
  114. }
  115. Date overdate = new Date();
  116. double time = overdate.getTime() - begindate.getTime();
  117. System.out.println("总耗时:" + time / 1000 + "s");
  118. } catch (Exception e) {
  119. System.out.println("下载失败");
  120. }
  121. }
  122. }

  

java简单web爬虫(网页图片)的更多相关文章

  1. java爬虫-简单爬取网页图片

    刚刚接触到“爬虫”这个词的时候是在大一,那时候什么都不明白,但知道了百度.谷歌他们的搜索引擎就是个爬虫. 现在大二.再次燃起对爬虫的热爱,查阅资料,知道常用java.python语言编程,这次我选择了 ...

  2. Python爬虫 网页图片

    一 概述 参考http://www.cnblogs.com/abelsu/p/4540711.html 弄了个Python捉取单一网页的图片,但是Python已经升到3+版本了.参考的已经失效,基本用 ...

  3. 从urllib和urllib2基础到一个简单抓取网页图片的小爬虫

    urllib最常用的两大功能(个人理解urllib用于辅助urllib2) 1.urllib.urlopen() 2. urllib.urlencode()   #适当的编码,可用于后面的post提交 ...

  4. node爬虫 -- 网页图片

    相信大家都听说过爬虫,我们也听说过Python是可以很方便地爬取网络上的图片,但是奈何本人不会Python,就只有通过 Node 来实践一下了.   接下来看我如何 板砖 ! !!   

  5. Python3简单爬虫抓取网页图片

    现在网上有很多python2写的爬虫抓取网页图片的实例,但不适用新手(新手都使用python3环境,不兼容python2), 所以我用Python3的语法写了一个简单抓取网页图片的实例,希望能够帮助到 ...

  6. Python并发编程-一个简单的爬虫

    一个简单的爬虫 #网页状态码 #200 正常 #404 网页找不到 #502 504 import requests from multiprocessing import Pool def get( ...

  7. Java简单爬虫(一)

    简单的说,爬虫的意思就是根据url访问请求,然后对返回的数据进行提取,获取对自己有用的信息.然后我们可以将这些有用的信息保存到数据库或者保存到文件中.如果我们手工一个一个访问提取非常慢,所以我们需要编 ...

  8. JAVA多线程超时加载当网页图片

    先上图: 这一次没有采取正则匹配,而采取了最简单的java分割和替代方法进行筛选图片 它能够筛选如下的图片并保存到指定的文件夹 如: “http://xxxx/xxxx/xxx.jpg” 'http: ...

  9. Python爬虫之网页图片抓取

    一.引入 这段时间一直在学习Python的东西,以前就听说Python爬虫多厉害,正好现在学到这里,跟着小甲鱼的Python视频写了一个爬虫程序,能实现简单的网页图片下载. 二.代码 __author ...

随机推荐

  1. The twentyth day

    10th Dec 2018 Cause It's hard for me to lose in my life I've found  因为失去你是一种煎熬 Only time will tell a ...

  2. 创建简单的node服务器

    昨天咱们说了封装ajax,今天咱们说一下 自己创建一个建议的node服务器: 话不多说直接上代码: var http = require('http') //对URL 解析为对象//1.导入模块 UR ...

  3. 菜鸟学习Spring——SpringIoC容器基于三种配置的对比

    一.概述 对于实现Bean信息定义的目标,它提供了基于XML.基于注解及基于java类这三种选项.下面总结一下三种配置方式的差异. 二.Bean不同配置方式比较. 三.Bean不同配置方式的适用场合. ...

  4. linux 中环境变量配置文件说明

    1. 修改/etc/profile文件 特点:所有用户的shell都有权使用你配置好的环境变量 说明:如果你的电脑仅用作开发,建议使用此配置,因为所有用户的shell都有权使用你配置好的环境变量,所以 ...

  5. android的MVP模式

    MVP简介 相信大家对MVC都是比较熟悉了:M-Model-模型.V-View-视图.C-Controller-控制器,MVP作为MVC的演化版本,那么类似的MVP所对应的意义:M-Model-模型. ...

  6. js:JavaScript中的ActiveXObject对象

    JavaScript中的ActiveXObject对象作用: https://blog.csdn.net/pl1612127/article/details/77862174

  7. 把IDENTITY_INSERT 设置为 ON ,还不能插入数据问题

    IDENTITY_INSERT 为 ON 时 , 必须把需要插入的列名列出来 不然报错 正确例子: SET IDENTITY_INSERT  table(表名) ONinsert into table ...

  8. zan-framework mysql连接

    ①根据文档内容要配置sqlmap连接池的读写白名单 http://doc.zanphp.io/zh/libs/connection_pool.html 示例代码 // demo.demo.demo_s ...

  9. TP5.0:新建控制器

    例如,我们在admin模块下创建一个名为OneMenu.php的控制器 1.在该控制器文件中内容为: 2.访问的URL为:http://localhost/tp5/public/index.php/a ...

  10. March 2 2017 Week 9 Thursday

    The first duty of love is to listen. 爱的首要责任是倾听. Yesterday, I read an article that says a successful ...