91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

使用Java語言實現爬蟲,用來爬取某個網站上的圖片 Httpclient負責發送請求 Jsoup負責解析 完整案例 可直接運行 有爬蟲思路解析圖

資源截圖

代碼片段和文件信息

package?com;

import?java.io.ByteArrayOutputStream;
import?java.io.File;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.net.HttpURLConnection;
import?java.net.MalformedURLException;
import?java.net.URL;
import?java.util.ArrayList;

import?org.apache.http.HttpEntity;
import?org.apache.http.client.ClientProtocolException;
import?org.apache.http.client.config.RequestConfig;
import?org.apache.http.client.methods.CloseableHttpResponse;
import?org.apache.http.client.methods.HttpGet;
import?org.apache.http.impl.client.CloseableHttpClient;
import?org.apache.http.impl.client.HttpClients;
import?org.apache.http.util.EntityUtils;
import?org.jsoup.Jsoup;
import?org.jsoup.nodes.Document;
import?org.jsoup.select.Elements;

/**
?*?
?*?@ClassName:?ImgUtil
?*?@Description:?TODO(爬蟲工具)
?*?@author?Admin
?*?@date?2018年6月17日
?*
?*/
public?class?ImgUtil?{

//?定義一個url地址
private?static?String?url?=?“http://www.netbian.com/desk/20747.htm“;

/**
?*?
?*?@title:?main?@Description:?TODO(這個類的入口)?@param?@param?args?參數?@return
?*?????????void?返回類型?@throws
?*/
public?static?void?main(String[]?args)?{
String?code?=?null;
ArrayList?list?=?null;
System.out.println(“開始“);
for(int?i?=?0;?i<10;?i++){
code?=?getCodeByUrl(url);//獲取源代碼
list=?getImgListFromCode(code);//獲取圖片的地址的集合
saveImgList(list);//將圖片保存到本地
getNextFromCode(code);//解析源代碼獲取下一頁地址
}
System.out.println(“結束“);
}
/**
?*?
*?@title:?getNextFromCode??
*?@Description:?TODO(解析源代碼獲取下一頁地址)??
*?@param?@param?code????參數??
*?@return?void????返回類型??
*?@throws
?*/
public?static?void?getNextFromCode(String?code){
//解析源代碼獲取下一頁地址
//頁面一定是一個Document對象
//創建一個Document對象
Document?document?=?Jsoup.parse(code);
//節點??獲取需要的節點
Elements?elements?=?document.select(“div[class=list]“).select(“a“);
int?k?=?elements.size();
for(int?i?=?0?;?i? String?urlNew?=?elements.get(i).attr(“href“);
if(i?==?0){
url?=?“http://www.netbian.com“?+?urlNew;
}
}
}
/**
?*?
*?@title:?saveImgList??
*?@Description:?TODO(遍歷圖片的地址集合??將圖片保存到本地)??
*?@param?@param?list????參數??
*?@return?void????返回類型??
*?@throws
?*/
public?static?void?saveImgList(ArrayList?list){
//遍歷圖片的地址集合??將圖片保存到本地
int?k?=?list.size();
String?src?=?null;//圖片的地址
//定義文件
File?file?=?null;
//定義URL對象
URL?url?=?null;
//定義URL的連接
HttpURLConnection?httpURLConnection?=?null;
//創建輸入流
InputStream?inputStream?=?null;
//創建文件輸出流
FileOutputStream?fileOutputStream?=?null;
try?{
for(int?i?=?0;?i? src?=?list.get(i);
//將src轉換成url地址
url?=?new?URL(src);
//建立連接
httpURLConnection?=?(HttpURLConnection)?url.openConnection();
//定義連接方式
httpURLConnection.setRequestMethod(“GET“);
//設置請求的超時信息
httpURLConnection.setConnectTimeout(5000);
//獲取文件輸入流
inputStream?=?httpURLConnection.getInputStream();

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\GetImgs\
?????文件?????????836??2018-06-17?18:56??Java爬蟲爬取圖片\GetImgs\.classpath
?????文件????????1040??2018-06-17?18:56??Java爬蟲爬取圖片\GetImgs\.project
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\GetImgs\.settings\
?????文件?????????567??2018-06-17?18:56??Java爬蟲爬取圖片\GetImgs\.settings\.jsdtscope
?????文件?????????364??2018-06-17?18:56??Java爬蟲爬取圖片\GetImgs\.settings\org.eclipse.jdt.core.prefs
?????文件?????????473??2018-06-17?18:56??Java爬蟲爬取圖片\GetImgs\.settings\org.eclipse.wst.common.component
?????文件?????????345??2018-06-17?18:56??Java爬蟲爬取圖片\GetImgs\.settings\org.eclipse.wst.common.project.facet.core.xml
?????文件??????????49??2018-06-17?18:56??Java爬蟲爬取圖片\GetImgs\.settings\org.eclipse.wst.jsdt.ui.superType.container
?????文件???????????6??2018-06-17?18:56??Java爬蟲爬取圖片\GetImgs\.settings\org.eclipse.wst.jsdt.ui.superType.name
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\GetImgs\WebContent\
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\GetImgs\WebContent\meta-INF\
?????文件??????????39??2018-06-17?18:56??Java爬蟲爬取圖片\GetImgs\WebContent\meta-INF\MANIFEST.MF
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\GetImgs\WebContent\WEB-INF\
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\GetImgs\WebContent\WEB-INF\lib\
?????文件???????61829??2018-06-17?19:25??Java爬蟲爬取圖片\GetImgs\WebContent\WEB-INF\lib\commons-logging-1.2.jar
?????文件??????766123??2018-06-17?19:25??Java爬蟲爬取圖片\GetImgs\WebContent\WEB-INF\lib\httpclient-4.5.5.jar
?????文件??????325491??2018-06-17?19:25??Java爬蟲爬取圖片\GetImgs\WebContent\WEB-INF\lib\httpcore-4.4.9.jar
?????文件??????395748??2018-06-17?19:26??Java爬蟲爬取圖片\GetImgs\WebContent\WEB-INF\lib\jsoup-1.11.3.jar
?????文件?????????650??2018-06-17?18:56??Java爬蟲爬取圖片\GetImgs\WebContent\WEB-INF\web.xml
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\GetImgs\build\
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\GetImgs\build\classes\
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\GetImgs\build\classes\com\
?????文件????????6266??2018-06-17?22:06??Java爬蟲爬取圖片\GetImgs\build\classes\com\ImgUtil.class
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\GetImgs\src\
?????目錄???????????0??2018-06-17?22:15??Java爬蟲爬取圖片\GetImgs\src\com\
?????文件????????6591??2018-06-17?22:01??Java爬蟲爬取圖片\GetImgs\src\com\ImgUtil.java
?????文件???????52756??2018-05-01?22:22??Java爬蟲爬取圖片\爬蟲思路.jpg

評論

共有 條評論