上一篇:Java網絡爬蟲實操(8)git
你們好,上一篇講到了下載器。這篇介紹爬蟲框架NetDiscovery新增的FileDownloader下載器,用於下載二進制流資源(office文檔、rar壓縮包、圖片等)的下載器。github
package com.cv4j.netdiscovery.example;
import com.cv4j.netdiscovery.core.Spider;
import com.cv4j.netdiscovery.core.domain.Request;
import com.cv4j.netdiscovery.core.downloader.file.FileDownloadAfterRequest;
import com.cv4j.netdiscovery.core.downloader.file.FileDownloader;
public class DownloadFiles {
public static void main(String[] args) {
//一、文件下載後存儲的目錄(不存在的話,程序會自動建立的)
String filePath = "D:\\savedir";
//二、指定資源的路徑和存儲文件名
//也能夠參考FileDownloadAfterRequest類自定義parser類,實現下載功能
Request request = new Request("http://cdn2.jianshu.io/assets/web/nav-logo-4c7bbafe27adc892f3046e6978459bac.png");
request.afterRequest(new FileDownloadAfterRequest(filePath, "jianshu.png"));
//3. 構建一個爬蟲,設置好下載器FileDownloader
Spider.create()
.request(request)
.downloader(new FileDownloader())
.run();
}
}
複製代碼
下載成功 web
package com.cv4j.netdiscovery.example;
import com.cv4j.netdiscovery.core.Spider;
import com.cv4j.netdiscovery.core.domain.Request;
import com.cv4j.netdiscovery.core.downloader.file.FileDownloadAfterRequest;
import com.cv4j.netdiscovery.core.downloader.file.FileDownloader;
public class DownloadFiles {
public static void main(String[] args) {
Request request = new Request("http://jsdx.sc.chinaz.com/Files/DownLoad/moban/201803/zppt2624.rar");
request.afterRequest(new FileDownloadAfterRequest("D:\\savedir", "1.rar"));
Spider.create()
.request(request)
.downloader(new FileDownloader())
.run();
}
}
複製代碼
下載成功 數組
package com.cv4j.netdiscovery.example;
import com.cv4j.netdiscovery.core.Spider;
import com.cv4j.netdiscovery.core.domain.Request;
import com.cv4j.netdiscovery.core.downloader.file.FileDownloadAfterRequest;
import com.cv4j.netdiscovery.core.downloader.file.FileDownloader;
public class DownloadFiles {
public static void main(String[] args) {
//一次下載3個資源
String[] urls = {"http://jsdx.sc.chinaz.com/Files/DownLoad/moban/201803/zppt2615.rar"
,"http://jsdx.sc.chinaz.com/Files/DownLoad/moban/201803/zppt2639.rar"
,"http://jsdx.sc.chinaz.com/Files/DownLoad/moban/201803/zppt2624.rar"};
String filePath = "D:\\savedir";
//構建Request數組
Request[] requests = new Request[urls.length];
for (int i = 0; i < requests.length; i++) {
String fileName = (i+1)+".rar";
requests[i] = new Request(urls[i])
.afterRequest(new FileDownloadAfterRequest(filePath, fileName));
}
Spider.create()
.request()
.downloader(new FileDownloader())
.run();
}
}
複製代碼
下載成功bash
上一篇:Java網絡爬蟲實操(10)網絡