要做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+" "); 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 }