JAVA实现一个简单的爬虫

网络爬虫是一种自动获取网页内容的程序,是搜索引擎的重要组成部分。

用java打造属于自己的爬虫

网络上的图片太多 一个一个的保存实在太浪费时间

基于此 就使用了java做了一个小工具

功能是文件的复制 以及 网络上图片的下载

首先是封装一个IOUtlis类方法,用于实现文件的复制和文件传输流的关闭

package hh;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.*;
public class IOUtils 
{
//	buff缓冲区

public static void closeQuietly(FileOutputStream outpustream)
{
	 if(outpustream!=null)
	 {
      
		 
    try
	{
    	outpustream.close();
	} 
      
      
      catch (IOException e)
	{
		// TODO 自动生成的 catch 块
		//e.printStackTrace();
	}
		 
    
		 
	 }
	 
	 
}
public static void closeQuietly(FileInputStream inStream)
{
	 if(inStream!=null)
	 {
      
		 
    try
	{
		inStream.close();
	} 
      
      
      catch (IOException e)
	{
		// TODO 自动生成的 catch 块
		//e.printStackTrace();
	}
		 
    
		 
	 }
	 
	 
}
public static void closeQuietly(Reader reader)
{
	 if(reader!=null)
	 {
		 try
		 {
			 reader.close();
			 
		 }
		 catch(IOException e )
		 {
			 //
			 
		 }
		 
		 
	 }
	 
	 
}

public static void copy(InputStream instream,OutputStream outputstream,int  buff) throws IOException
{
	byte[] buffer=new byte[buff];
	int len;//计算读入的数据
	if(instream==null)
	{
		throw new IllegalArgumentException("INPUTSTREAM不能为空");
		
	}
	if(outputstream==null)
	{
		throw new IllegalArgumentException("outputstream不能为空");
		
	}
	if(buff<=0)
	{
		
		throw new IllegalArgumentException("outputstream不能为空");

		
	}
	while((len=instream.read(buffer))>0)
	{
			//不知道怎么处理异常就让调用者处理
		outputstream.write(buffer,0,len);
		
	}

}

public static void copy(InputStream instream,OutputStream outputstream) throws IOException
{
	
copy(instream,outputstream,1024);
}

 copy这个方法实现了文件的复制功能,closequietly将文件流安静的关闭

方案一

文件的复制,目的是将E盘的1.txt文件复制到W盘的2.txt文件里 最后不要忘了关闭文件的输入输出流

不然可能导致复制失败

package hh;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class test {

	public static void main(String[] args)
	{
		long startMS=System.currentTimeMillis();
		
		FileInputStream fis=null;
		FileOutputStream fos=null;
		
		
		try
		{
			fis=new FileInputStream("E:\\1.txt");
			fos=new FileOutputStream("W:\\2.txt");
			IOUtils.copy(fis,fos,1024);
			long endMS=System.currentTimeMillis();
			System.out.println("拷贝完成 耗时"+(endMS-startMS));
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
		
	}

  方案二

   调用了java.net.*;这个包 使用url类将图片地址比如下图画圈部分当做一个文件的输入流来使用,再使用copy方法将图片复制到本地。最后我发现该网站所有的图片都有一个特定的图片递增规则(可以考虑使用正则表达式)这样就可以通过变量来完成网站里所有的图片的下载

 

 

package hh;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
public class abc {


	public static void main(String []args)
	{
		  InputStream inpustream=null;
	      FileOutputStream outputstream=null;

		try {
			long start=System.currentTimeMillis();
		int i=0;
		for(i=1;i<550;i++)
		{
			URL url=new URL("http://www.bokee.com//topic/images/%E6%9C%AA%E6%A0%87%E9%A2%98-2("+i+").jpg");
	    	  inpustream=url.openStream();
	    	  
//	    	  http://www.bokee.com//topic/images/%E6%9C%AA%E6%A0%87%E9%A2%98-1(530).jpg
//	    	  <img src="/_upload/article/images/66/b6/810dfe294d62aa81fba5ca1baef9/57c958b6-72e8-4331-a431-24b06039b649.jpg">
//	    	  <img src="/_upload/article/images/99/a1/416529614b32ba6a2f824bbbafae/04b85865-f1b9-4128-b33f-04eff1e81c5a.jpg">
	          outputstream=new FileOutputStream("W://main//"+i+".png");
		      IOUtils.copy(inpustream, outputstream);
		}
		long end=System.currentTimeMillis();

		System.out.println("下载成功,耗时"+(start-end)+"毫秒");
		} 
		
		catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	
	
	
	
	}
	

  

 成果如下,最终满足了自己的需求,节省了时间。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值