由于今天我要保存一个网页上的所有图片并做一个ppt,但是这个网页比较蛋疼,是微信上的一个类似于动画的东西,所以没法保存整个网页然后直接取照片,
所以我采用java写了一个程序,采用类似于网络爬虫的思路把照片的抓下来。
网页链接如下
/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 .*;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
public class CatchPic {
BufferedImage image = null;
String picUrl = "/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("/weather/10111.shtml"); //jinan
// String temp = getURLContent("/weather1d/101210101.shtml"); //hangzhou
String url = "/topic/liushouh5/?from=timeline&isappinstalled=0";
String temp = getURLContent(url);
new CatchPic().findAllPic("jpg");
}
public void findAllPic(String format){
String url = "/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 = "/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如果要应用到其他网页需要改动一些东西,这个程序针对这个网页/topic/liushouh5/?from=timeline&isappinstalled=0做了一些优化
如果觉得《java抓取并保存图片_利用JAVA抓取网站的所有图片并保存于本地》对你有帮助,请点赞、收藏,并留下你的观点哦!