封装抓取网页信息的实例


  1. package cn.mypic;  
  2.   
  3.   
  4.     import java.io.BufferedInputStream;  
  5.     import java.io.BufferedReader;  
  6.     import java.io.File;  
  7.     import java.io.FileNotFoundException;  
  8.     import java.io.FileOutputStream;  
  9.     import java.io.IOException;  
  10.     import java.io.InputStreamReader;  
  11.     import java.net.MalformedURLException;  
  12.     import java.net.URL;  
  13.     import java.util.regex.Matcher;  
  14.     import java.util.regex.Pattern;  
  15.   
  16.   
  17.     public class GetContentPicture {  
  18.     //得到了图片地址并下载图片  
  19.     public void getHtmlPicture(String httpUrl) {  
  20.     URL url;  
  21.     BufferedInputStream in;  
  22.     FileOutputStream file;  
  23.     int count;                      //图片文件名序号   
  24.     FileNumber num=new FileNumber();//图片文件名序号类,num为对象  
  25.     count=num.NumberReadFromFile();//获取图片文件序号  
  26.     try {  
  27.     System.out.println("获取网络图片");  
  28.        String fileName = (String.valueOf(count)).concat(httpUrl.substring(httpUrl.lastIndexOf(".")));//图片文件序号加上图片的后缀名,后缀名用了String内的一个方法来获得  
  29.         //httpUrl.substring(httpUrl.lastIndexOf("/"));//这样获得的文件名即是图片链接里图片的名字  
  30.        String filePath = "d:/image/";//图片存储的位置  
  31.        url = new URL(httpUrl);  
  32.   
  33.        in = new BufferedInputStream(url.openStream());  
  34.   
  35.        file = new FileOutputStream(new File(filePath+fileName));  
  36.        int t;  
  37.        while ((t = in.read()) != -1) {  
  38.         file.write(t);  
  39.        }  
  40.        file.close();  
  41.        in.close();  
  42.        System.out.println("图片获取成功");  
  43.        count=count+1;//图片文件序号加1  
  44.        num.NumberWriteToFile(count);//将图片名序号保存  
  45.     } catch (MalformedURLException e) {  
  46.        e.printStackTrace();  
  47.     } catch (FileNotFoundException e) {  
  48.        e.printStackTrace();  
  49.     } catch (IOException e) {  
  50.        e.printStackTrace();  
  51.     }  
  52.     }  
  53.   
  54.     //获取网页的代码保存在String格式的Content中  
  55.     public String getHtmlCode(String httpUrl) throws IOException {  
  56.     String content ="";  
  57.     URL uu = new URL(httpUrl); // 创建URL类对象  
  58.     BufferedReader ii = new BufferedReader(new InputStreamReader(uu  
  59.         .openStream())); // //使用openStream得到一输入流并由此构造一个BufferedReader对象  
  60.     String input;  
  61.     while ((input = ii.readLine()) != null) { // 建立读取循环,并判断是否有读取值  
  62.        content += input;  
  63.     }  
  64.     ii.close();  
  65.     return content;  
  66.     }  
  67.     //分析网页代码,找到匹配的网页图片地址  
  68.     public void get(String url) throws IOException {  
  69.   
  70.     String searchImgReg = "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";//用于在网页代码Content中查找匹配的图片链接。  
  71.     String searchImgReg2 = "(?x)(src|SRC|background|BACKGROUND)=('|\")(http://([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/[\\w-]+)*(/[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";  
  72.   
  73.     String content = this.getHtmlCode(url);//this指对象gcp,在此地调用获取网页代码,getHtmlCode方法  
  74.     //System.out.println(content); //输出的content将是一个连续的字符串。  
  75.   
  76.     Pattern pattern = Pattern.compile(searchImgReg);//java.util.regex.Pattern  
  77.     Matcher matcher = pattern.matcher(content);     //java.util.regex.Matcher  
  78.     while (matcher.find()) {  
  79.        System.out.println(matcher.group(3));//输出图片链接地址到屏幕  
  80.     // System.out.println(url);  
  81.        this.getHtmlPicture(matcher.group(3));//对象调用getHtmlPicture从网上下载并输出图片文件到指定目录  
  82.   
  83.     }  
  84.   
  85.     pattern = Pattern.compile(searchImgReg2);  
  86.     matcher = pattern.matcher(content);  
  87.     while (matcher.find()) {  
  88.        System.out.println(matcher.group(3));  
  89.        this.getHtmlPicture(matcher.group(3));  
  90.   
  91.     }  
  92.     // searchImgReg =  
  93.     // "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";  
  94.     }  
  95.     //主函数url网页的地址  
  96.     public static void main(String[] args) throws IOException {  
  97.   
  98.     String url = "http://www.baidu.com";  
  99.     GetContentPicture gcp = new GetContentPicture();  
  100.     gcp.get(url);  
  101.       
  102.   
  103.     }  
  104.   
  105.   
  106. }  

 

 

Java代码  收藏代码
  1. package cn.mypic;  
  2.   
  3.     import java.io.*;  
  4.   
  5.     public class FileNumber{  
  6.     //文件写  
  7.     public void NumberWriteToFile(int x){  
  8.        int c=0;  
  9.        c=x;  
  10.        File filePath=new File("d:/image");//文件名序号TXT文件保存地址  
  11.        File f1=new File(filePath,"number.txt");  
  12.        try{  
  13.         FileOutputStream fout=new FileOutputStream(f1);  
  14.         DataOutputStream out=new DataOutputStream(fout);  
  15.         out.writeInt(c);  
  16.          
  17.        }  
  18.        catch(FileNotFoundException e){  
  19.         System.err.println(e);  
  20.        }  
  21.        catch(IOException e){  
  22.         System.err.println(e);  
  23.        }  
  24.         
  25.     }  
  26.     //文件读  
  27.     public int NumberReadFromFile(){  
  28.        int c1 = 0;  
  29.        File filePath=new File("d:/image");  
  30.        File f1=new File(filePath,"number.txt");  
  31.        try{  
  32.         FileInputStream fin=new FileInputStream(f1);  
  33.         DataInputStream in=new DataInputStream(fin);  
  34.         c1=in.readInt();  
  35.         System.out.println(c1);//输出文件内容至屏幕  
  36.        }  
  37.        catch(FileNotFoundException e){  
  38.         System.err.println(e);  
  39.        }  
  40.        catch(IOException e){  
  41.         System.err.println(e);  
  42.        }  
  43.        return c1;  
  44.     }  
  45.     public static void main(String args[]){  
  46.         
  47.     }  
  48.   
  49.   
  50.   

posted on 2013-03-11 14:44  jackrex  阅读(237)  评论(0编辑  收藏  举报

导航