JAVA簡單爬蟲例子--Jsoup的運用

突然發現爬蟲挺有意思的,開始研究Java的爬蟲框架,作爲簡單的入門,覺得jsoup還是比較適合初學者,下面就是爬天堂圖片的圖片的例子

Jsoupde 中文文檔 http://www.open-open.com/jsoup/ 參考


1、根據頁面源碼提取圖片的url
2、根據url下載圖片

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.net.URL;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JsoupImgDown {

    public static void main(String[] args) {

        try {
            for(int i=0;i<9;i++){
                int k=80+i;
                Document doc = Jsoup.connect("http://www.ivsky.com/tupian/nvsheng_v39311/pic_6340"+k+".html").get();

                // String title = doc.title();
                if(doc!=null){
                    String path=doc.getElementById("imgis").attr("src");
                    System.out.println(path);
                    downPic(path,k+"b");
                }
            }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public static void downPic(String path,String filename) throws IOException {
            URL url = new URL(path);

            URLConnection uc = url.openConnection();
            uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            InputStream is = uc.getInputStream();
            byte[] bs = new byte[1024];
            FileOutputStream out = new FileOutputStream("E:\\temp\\"+filename+".jpg");
            int i = 0;
            while ((i = is.read(bs)) != -1) {
                out.write(bs,0,i);
            } 
            is.close();
            out.close();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章