java crawling on picture cat

The first is the knowledge points inductive

1. used to obtain the source code pages, picture address analysis, the address is found Pictures are numbered, so the thought of obtaining by cyclic

2. Save images to use to stream operations and file operations, knowledge of the two parts of the review and consolidate

3. Save the picture in part, ad text, so I looked at how to intercept picture

On the following codes:

Web page source code is not pasted

After pages of source code analysis cycle to extract images and download links:

  1 package 文件操作;
  2 
  3 import java.io.ByteArrayOutputStream;
  4 import java.io.File;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 import java.net.HttpURLConnection;
  9 import java.net.MalformedURLException;
 10 import java.net.URL;
 11 
 12 public class Pic696 {
 13 
 14     public static void main(String[] args) {
 15     for(int i =1;i<72;i++) {
 16         for (int j =1; j < 50; j++) {
 17 
 18             if(i<10&&j<10) {
 19                 String strUrl ="https://mmslt1.com/tp/girl/FEILIN/A-00"+i+"/0"+j+".jpg";
 20 //                System.out.println(i+" "+j);
 21                 System.out.println(strUrl);
 22                 URL url;
 23                 try {
 24                     url = new URL(strUrl);
 25                     try {
 26                         Pig(url, i, j);
 27                     } catch (IOException e) {
 28                         System.out.println("404");
 29                     }
 30                 } catch (MalformedURLException e) {
 31                     
 32                 }
 33                 
 34             }
 35             if(i<10&&j>=10) {
 36                 String strUrl ="https://mmslt1.com/tp/girl/FEILIN/A-00"+i+"/"+j+".jpg";
 37 //                System.out.println(i+" "+j);
 38                 System.out.println(strUrl);
 39                 URL url;
 40                 try {
 41                     url = new URL(strUrl);
 42                     try {
 43                         Pig(url, i, j);
 44                     } catch (IOException e) {
 45                         System.out.println("404");
 46                     }
 47                 } catch (MalformedURLException e) {
 48                     // TODO Auto-generated catch block
 49                     e.printStackTrace();
 50                 }
 51                 
 52             }
 53             if(i>=10&&j<10) {
 54                 String strUrl ="https://mmslt1.com/tp/girl/FEILIN/A-0"+i+"/0"+j+".jpg";
 55 //                System.out.println(i+" "+j);
 56                 System.out.println(strUrl);
 57                 URL url;
 58                 try {
 59                     url = new URL(strUrl);
 60                     try {
 61                     Pig(url, i, j);
 62                 } catch (IOException e) {
 63                     System.out.println("404");
 64                 }
 65                 } catch (MalformedURLException e) {
 66                     // TODO Auto-generated catch block
 67                     e.printStackTrace();
 68                 }
 69                 
 70             }
 71             if(i>=10&&j>=10) {
 72             String strUrl ="https://mmslt1.com/tp/girl/FEILIN/A-0"+i+"/"+j+".jpg";
 73 //            System.out.println(i+" "+j);
 74             System.out.println(strUrl);
 75             URL url;
 76             try {
 77                 url = new URL(strUrl);
 78                 try {
 79                 Pig(url, i, j);
 80             } catch (IOException e) {
 81                 System.out.println("404");
 82             }
 83             } catch (MalformedURLException e) {
 84                 // TODO Auto-generated catch block
 85                 e.printStackTrace ();
 86              }
 87              
88              }
 89          }
 90      }
 91 is              
92  }
 93      
94      static  void Pig (urlStr the URL, int I, int J) throws IOException {
 95          // configured connection 
96          the HttpURLConnection Conn = (the HttpURLConnection) urlStr. openConnection ();
 97          conn.setRequestMethod ( "GET" );
 98          // this site to simulate the job browser 
99         conn.setRequestProperty ( "the User-- Agent", "the Mozilla / 5.0 (the Windows NT 10.0; the WOW64; RV: 46.0) the Gecko / Firefox 20,100,101 / 46.0" );
 100          // open connection 
101          conn.connect ();
 102  
103          // open the site input stream 
104          the InputStream inStream = conn.getInputStream ();
 105          // do with this relay station, the picture data are placed here, then call the toByteArray () to get the data byte array 
106          ByteArrayOutputStream the outStream = new new ByteArrayOutputStream ();
 107          // use this is good, do not put a picture file read
 108          // if the pictures need to be used for other purposes? So the picture ended up data directly to a variable, useful
 109          // corresponds to the operation of this variable will be able to operate the picture
110  
111          byte [] = buf new new  byte [1024 ];
 112          // Why 1024?
113          // 1024Byte = 1KB, 1KB allocated cache 
114          
115          int len = 0 ;
 1 16  
117          // reading the image data 
1 18          the while - ((len = inStream.read (buf)) =. 1! {)
 119  //         the System. Out.println (len); 
120          outStream.write (buf, 0 , len);
 121          }
 122          inStream.close ();
 123          outStream.close ();
 124  
125          //把图片数据填入文件中
126 //        File files = new File("E://Pic696");
127 //        files.mkdirs();
128         File file = new File("E://Pic696/Pic"+i+"_"+j+".jpg");
129 
130         FileOutputStream op = new FileOutputStream(file);
131 
132         op.write(outStream.toByteArray());
133 
134         op.close();
135      }    
136 }

To download the picture interception (get the original size, lose part)

 1 package 文件操作;
 2 
 3 import java.awt.Rectangle;
 4 import java.awt.image.BufferedImage;
 5 import java.io.BufferedReader;
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.IOException;
 9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.net.MalformedURLException;
12 import java.net.URL;
13 import java.util.Iterator;
14 
15 import javax.imageio.ImageIO;
16 import javax.imageio.ImageReadParam;
17 import javax.imageio.ImageReader;
18 import javax.imageio.stream.ImageInputStream;
19 import javax.swing.ImageIcon;
20 
21 
22 
23 public class cityScore {
24  
25     public static void main(String[] args) {
26         for (int i = 1; i < 10; i++) {
27             for (int j = 1; j < 40; j++) {
28                 String OldPic ="E:/Pic696/Pic"+i+"_"+j+".jpg";
29                 String NewPic ="E:/Pic696/new/Pic"+i+"_"+j+".jpg";
30                 ImageIcon imageIcon = new ImageIcon(OldPic);
31                 int iconWidth = imageIcon.getIconWidth();
32                 int iconHeight =imageIcon.getIconHeight();
33                 cutImage(OldPic, NewPic,iconWidth, iconHeight-70);
34             }
35         }
36         
37     }
38     
39     /**
40      * Cropped image
 41 is       * @param srcImageFile address cropped image
 42 is       * @param Result output image folder
 43 is       * @param destWidth image cropping width
 44 is       * @param destHeight crop the image height
 45       * / 
46 is      public  Final  static  void cutImage (srcImageFile String, String Result,
 47              int destWidth, int destHeight) {
 48          the try {
 49              the Iterator Iterator = ImageIO.getImageReadersByFormatName ( "the JPEG"); / * PNG, BMP * /  
50             ImageReader reader = (ImageReader)iterator.next();/*获取图片尺寸*/
51             InputStream inputStream = new FileInputStream(srcImageFile); 
52             ImageInputStream iis = ImageIO.createImageInputStream(inputStream);  
53             reader.setInput(iis, true);  
54             ImageReadParam param = reader.getDefaultReadParam();  
55             Rectangle rectangle = new Rectangle(0,0, destWidth, destHeight);/*指定截取范围*/   
56             param.setSourceRegion(rectangle);  
57             BufferedImage bi = reader.read(0,param);
58             ImageIO.write(bi, "JPEG", new File(result));
59         } catch (Exception e) {
60             System.out.println(e);
61         }
62     }
63 }

 

Guess you like

Origin www.cnblogs.com/fangmr/p/11266873.html