java抓取并保存图片_利用JAVA抓取网站的所有图片并保存于本地
由于今天我要保存一个网页上的所有图片并做一个ppt,但是这个网页比较蛋疼,是微信上的一个类似于动画的东西,所以没法保存整个网页然后直接取照片,
所以我采用java写了一个程序,采用类似于网络爬虫的思路把照片的抓下来。
网页链接如下
http://irm.infzm.com/topic/liushouh5/?from=timeline&isappinstalled=0
截图如下是一个可以左右翻页的网页
然后抓取图片的关键代码为
public static String getURLContent(String urlStr) {
/** 网络的url地址 */
URL url = null;
/** http连接 */
HttpURLConnection httpConn = null;
/**//** 输入流 */
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try{
url = new URL(urlStr);
in = new BufferedReader( new InputStreamReader(url.openStream(),"UTF-8") );
String str = null;
while((str = in.readLine()) != null) {
sb.append( str + "\n" );
}
} catch (Exception ex) {
} finally{
try{
if(in!=null) {
in.close();
}
}catch(IOException ex) {
}
}
String result =sb.toString();
// System.out.println(result);
return result;
}以上是获取网页源代码然后可以加以获取图片的链接
public void writeImageFile(BufferedImage bi,String name,String format) throws IOException{
File outputfile = new File(name+format);
ImageIO.write(bi, format, outputfile);
System.out.println("caught");
}
以上是把图片缓存写入磁盘上
public void readNetImage(String netPicture){
try {
URL url = new URL(netPicture);
//打开链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
image = ImageIO.read(inStream);
System.out.println("read");
} catch (IOException e) {
System.err.println("An error occured when loading the image icon...");
}
}
以上是读取网页上的图片
这个java文件:
package spring2;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
public class CatchPic {
BufferedImage image = null;
String picUrl = "http://cnpc.infzm.com/mostnorth/src/static/image/";
/**
* 程序中访问http数据接口
*/
public static String getURLContent(String urlStr) {
/** 网络的url地址 */
URL url = null;
/** http连接 */
HttpURLConnection httpConn = null;
/**//** 输入流 */
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try{
url = new URL(urlStr);
in = new BufferedReader( new InputStreamReader(url.openStream(),"UTF-8") );
String str = null;
while((str = in.readLine()) != null) {
sb.append( str + "\n" );
}
} catch (Exception ex) {
} finally{
try{
if(in!=null) {
in.close();
}
}catch(IOException ex) {
}
}
String result =sb.toString();
// System.out.println(result);
return result;
}
public static void main(String[] args){
// String temp = getURLContent("http://www.weather.com.cn/weather/101120101.shtml"); //jinan
// String temp = getURLContent("http://www.weather.com.cn/weather1d/101210101.shtml"); //hangzhou
String url = "http://irm.infzm.com/topic/liushouh5/?from=timeline&isappinstalled=0";
String temp = getURLContent(url);
new CatchPic().findAllPic("jpg");
}
public void findAllPic(String format){
String url = "http://irm.infzm.com/topic/liushouh5/?from=timeline&isappinstalled=0";
String temp = getURLContent(url);
int max = temp.lastIndexOf(format);
int index=0;
while(index
{
int one = temp.indexOf(format, index+1);
index = one;
while(temp.charAt(one--)!='/');
String test = temp.substring(one+2, index);
readNetImage(picUrl+test+format);
System.out.println(test+"\t"+picUrl+test+format);
try {
writeImageFile(image,test,format);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void writeImageFile(BufferedImage bi,String name,String format) throws IOException{
File outputfile = new File(name+format);
ImageIO.write(bi, format, outputfile);
System.out.println("caught");
}
public void readNetImage(String netPicture){
try {
URL url = new URL(netPicture);
//打开链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
image = ImageIO.read(inStream);
System.out.println("read");
} catch (IOException e) {
System.err.println("An error occured when loading the image icon...");
}
}
void special(String format){
String url = "http://irm.infzm.com/topic/liushouh5/?from=timeline&isappinstalled=0";
String temp = getURLContent(url);
int max = temp.lastIndexOf(format);
int index = max;
while(temp.charAt(max--)!='/');
String test = temp.substring(max+2, index);
readNetImage(picUrl+test+format);
System.out.println(test+"\t"+picUrl+test+format);
try {
writeImageFile(image,test,format);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行后保存的图片:
ps如果要应用到其他网页需要改动一些东西,这个程序针对这个网页http://irm.infzm.com/topic/liushouh5/?from=timeline&isappinstalled=0做了一些优化
java抓取并保存图片_利用JAVA抓取网站的所有图片并保存于本地相关推荐
- java导出mysql数据库失败_利用Java进行MySql数据库的导入和导出
利用Java来进行Mysql数据库的导入和导出的总体思想是通过Java来调用命令窗口执行相应的命令. MySql导出数据库的命令如下: mysqldump -uusername -ppassword ...
- java判断是否第一次出现_利用java判断字符首次出现的位置,java替换最后一个特定字符...
利用java判断字符首次出现的位置利用爪哇判断字符首次出现的位置, 目的: (学习视频分享:java视频教程 实现代码如下: 导入Java.util.收藏品: 导入Java.util.LinkedLi ...
- java se好用吗_利用 Java SE 7 更好地管理资源
2011 年 5 月发布 作者:Julien Ponge 本文介绍 Java 7 针对自动资源管理问题给出的解决办法,即 Coin 项目中提出的新语言结构 try-with-resources 语句. ...
- python抓取数据包_利用python-pypcap抓取带VLAN标签的数据包方法
1.背景介绍 在采用通常的socket抓包方式下,操作系统会自动将收到包的VLAN信息剥离,导致上层应用收到的包不会含有VLAN标签信息.而libpcap虽然是基于socket实现抓包,但在收到数据包 ...
- 在java里四舍五入怎么做_利用java怎么实现一个四舍五入功能
利用java怎么实现一个四舍五入功能 发布时间:2020-12-02 16:46:08 来源:亿速云 阅读:68 作者:Leah 这期内容当中小编将会给大家带来有关利用java怎么实现一个四舍五入功能 ...
- java运用到的单词_利用Java怎么实现一个单词提取功能
利用Java怎么实现一个单词提取功能 发布时间:2020-12-08 17:07:00 来源:亿速云 阅读:65 作者:Leah 这篇文章给大家介绍利用Java怎么实现一个单词提取功能,内容非常详细, ...
- java反序列化漏洞POP查找_利用 Java 反序列化漏洞在受限环境下获取反向 Shell
原标题:利用 Java 反序列化漏洞在受限环境下获取反向 Shell 原文链接: https://medium.com/abn-amro-red-team/java-deserialization-f ...
- java 怎么做302重定向_利用Java怎么获取302重定向后的URL
利用Java怎么获取302重定向后的URL 发布时间:2021-01-22 16:59:02 来源:亿速云 阅读:57 作者:Leah 本篇文章为大家展示了利用Java怎么获取302重定向后的URL, ...
- python爬取软件数据_利用Python爬取爬取APP上面的数据
前言 在我们在爬取手机APP上面的数据的时候,都会借助Fidder来爬取.今天就教大家如何爬取手机APP上面的数据. 环境配置 1.Fidder的安装和配置 下载Fidder软件地址:https:// ...
最新文章
- ogg oracle to mysql_ogg oracle to mysql
- 不想学python-为什么自学python总是坚持不下去,这篇文章给你解答!
- numpy 图片填充_numpy/python中的洪水填充分割图像
- 《系统集成项目管理工程师》必背100个知识点-03项目管理过程组
- 不混淆so文件_浅尝ollvm轻度混淆后的加密算法分析
- 百度SEO站群织梦dedecms自定义多条件筛选插件源码
- 单点登录的原理与简单实现
- [Git] Ubuntu 上更新 git
- 越界操作导致程序崩溃的原理
- 软考网络工程师-华为设备命令学习笔记
- 【分享】这款微信电子名片真的很值得拥有!
- 洛谷题单-【算法1-5】贪心
- 动作捕捉技术对演员的演技诉求
- Windows11/10 使用RDP远程桌面时提示 您的凭据不工作/登录没有成功可能的一种原因
- 基于标定板的手眼标定
- getElementsByTagName用法详解
- Vue 安装 Element UI时报错 code ERESOLVE unable to resolve dependency tree
- 股市入门篇——什么是熊市?
- IPv6 基本首部、地址和过渡
- 开源IM可以开发哪些产品和功能
热门文章
- android下拉刷新的方法,Android App使用RecyclerView实现上拉和下拉刷新的方法
- 基于SSM的图书进销存管理系统
- python爬图片_python爬取图片,保存图片到本地
- docker 快鸟_GitHub - fffonion/Xunlei-Fastdick: 迅雷快鸟 Xunlei Network Accelerator For Router
- linux 提取cpio_【rpm】从rpm包中提取文件:rpm2cpio和cpio的使用
- Linux系统下cpio命令详解
- forEach到底可以改变原数组吗
- 开源自制的6通道航模遥控器(一) 超简单不超过100行代码
- 微信投票(java)
- 解决“yyyy-MM-ddTHH:mm:ss+mm:ss” 格式日期转成Date类型