1. 程式人生 > >Java爬蟲

Java爬蟲

catch splay 保存圖片 lan current array .cn image aps

作為一位Java爬蟲的初學者,分享一下自己的心得。
所用到的jar包

org.codehaus.jettison.jar

jsoup-1.7.3.jar

個人認為爬蟲的實現機制:
獲取Docume對象—>獲取節點—>輸出或者持久化

獲取頁面的圖片地址

獲取Docume對象—>獲取Img元素—>輸出地址

技術分享
 1 package com.cn.basic;
 2 
 3 import java.io.IOException;
 4 import org.jsoup.Jsoup;
 5 import org.jsoup.nodes.Document;
 6 import org.jsoup.nodes.Element;
7 import org.jsoup.select.Elements; 8 9 public class ImageDemo1 { 10 11 public static void Get_Url(String htmlUrl, String path) { 12 13 try { 14 Document doc = Jsoup.connect(htmlUrl).get(); 15 16 Element body = doc.body(); 17 Elements elements = body.select("img");
18 19 String src = ""; 20 for (Element element : elements) { 21 22 src = element.attr("src"); 23 24 System.out.println(path + src); 25 26 } 27 28 System.out.println("elements-size: " + elements.size()); 29 30 } catch
(IOException e) { 31 e.printStackTrace(); 32 } 33 34 } 35 36 public static void main(String[] args) { 37 38 String url = "http://pic.netbian.com/4kkatongdongman/index_2.html"; 39 String path = "http://pic.netbian.com"; 40 Get_Url(url, path); 41 42 } 43 44 }
View Code

將圖片寫入本地

獲取Docume對象—>獲取Img元素—>將圖片保存本地

技術分享
  1 package com.cn.basic;
  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.URL;
 10 import java.util.Date;
 11 
 12 import org.jsoup.Jsoup;
 13 import org.jsoup.nodes.Document;
 14 import org.jsoup.nodes.Element;
 15 import org.jsoup.select.Elements;
 16 
 17 public class ImageDemo2 {
 18 
 19     public static void saveImage(String htmlUrl, String path) {
 20 
 21         try {
 22             Document doc = Jsoup.connect(htmlUrl).get();
 23             Element body = doc.body();
 24             Elements elements = body.select("img");
 25             
 26             String outputFilePath="E:/pythonTest/javaPython/imgs/";
 27             String src = "";
 28 
 29             HttpURLConnection conn = null;
 30             InputStream inStream = null;
 31             byte[] data = null;
 32             String filePath = null;
 33             FileOutputStream outStream = null;
 34             
 35             Long startTime=new Date().getTime();
 36             
 37             for (Element element : elements) {
 38 
 39                 src = element.attr("src");
 40 
 41                 System.out.println(path + src);
 42                 // new一個URL對象
 43 
 44                 if (!src.contains(".jpg")) {
 45                     continue;
 46                 }
 47 
 48                 URL url = new URL(path + src);
 49                 // 打開鏈接
 50                 conn = (HttpURLConnection) url.openConnection();
 51                 // 設置請求方式為"GET"
 52                 conn.setRequestMethod("GET");
 53                 // 超時響應時間為5秒
 54                 conn.setConnectTimeout(5 * 1000);
 55                 // 通過輸入流獲取圖片數據
 56                 inStream = conn.getInputStream();
 57                 // 得到圖片的二進制數據,以二進制封裝得到數據,具有通用性
 58                 data = readInputStream(inStream);
 59                 // new一個文件對象用來保存圖片,默認保存當前工程根目錄
 60                 filePath = outputFilePath + System.currentTimeMillis() + ".jpg";
 61                 // 創建輸出流
 62                 outStream = new FileOutputStream(new File(filePath));
 63                 // 寫入數據
 64                 outStream.write(data);
 65                 // 關閉輸出流
 66                 outStream.close();
 67 
 68             }
 69             System.out.println(elements.size());
 70             System.out.println("讀寫速度:"+(new Date().getTime()-startTime)+"毫秒");
 71             
 72 
 73         } catch (IOException e) {
 74             e.printStackTrace();
 75         } catch (Exception e) {
 76             e.printStackTrace();
 77         }
 78 
 79     }
 80 
 81     public static byte[] readInputStream(InputStream inStream) throws Exception {
 82         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
 83         // 創建一個Buffer字符串
 84         byte[] buffer = new byte[1024];
 85         // 每次讀取的字符串長度,如果為-1,代表全部讀取完畢
 86         int len = 0;
 87         // 使用一個輸入流從buffer裏把數據讀取出來
 88         while ((len = inStream.read(buffer)) != -1) {
 89             // 用輸出流往buffer裏寫入數據,中間參數代表從哪個位置開始讀,len代表讀取的長度
 90             outStream.write(buffer, 0, len);
 91         }
 92         // 關閉輸入流
 93         inStream.close();
 94         // 把outStream裏的數據寫入內存
 95         return outStream.toByteArray();
 96     }
 97 
 98     public static void main(String[] args) {
 99         String url = "http://pic.netbian.com/4kkatongdongman/index_2.html";
100         String path = "http://pic.netbian.com";
101         saveImage(url, path);
102     }
103 
104 }
View Code

Java爬蟲