1. 程式人生 > >【java】<Jsoup>獲取網頁中的圖片

【java】<Jsoup>獲取網頁中的圖片

util puts lec import http imp stat tin 畫的

要做Android課程設計了,做一個爬漫畫的東東練一下手

 1 package asd;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8 import java.net.URL;
 9 import java.net.URLConnection;
10 import java.util.ArrayList;
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 18 public class DownLoadPic { 19 20 private static final String saveImgPath="D://imgs"; 21 //圖片保存路徑 22 23 public void getDoc() throws IOException{ 24 ArrayList<String> ar = new
ArrayList<String>(); 25 int n=0; 26 String s; 27 while(n<=19){ 28 s="http://manhua.fzdm.com/22/636/index_"+n+".html"; 29 Document doc = Jsoup.connect(s).get(); 30 //獲取後綴為png和jpg的圖片的元素集合 31 Elements pngs=null; 32 pngs = doc.select("img[src~=(?i)\\.jpg]");
33 for(Element e : pngs){ 34 35 String src=e.attr("src");//獲取img中的src路徑 36 if(src.indexOf("http:")<0) 37 src="http:"+src; 38 ar.add(src); 39 break; 40 } 41 n++; 42 } 43 //遍歷元素 44 int imageName=0; 45 for(String e : ar){ 46 imageName=imageName+1; 47 System.out.print(e+"\n"); 48 URL url = new URL(e); 49 URLConnection uri=url.openConnection(); 50 //獲取數據流 51 InputStream is=uri.getInputStream(); 52 //寫入數據流 53 OutputStream os = new FileOutputStream(new File(saveImgPath, imageName+".jpg")); 54 byte[] buf = new byte[1024]; 55 int l=0; 56 while((l=is.read(buf))!=-1){ 57 os.write(buf, 0, l); 58 os.flush(); 59 } 60 } 61 62 63 } 64 65 66 public static void main(String[] args) throws IOException { 67 new DownLoadPic().getDoc(); //調用方法 68 } 69 }

【java】<Jsoup>獲取網頁中的圖片