JAVA使用HttpClient實現爬蟲技術

2021-08-17 17:15:49 字數 2383 閱讀 6971

1. pom檔案中加入httpclient依賴包

2. 建立乙個呼叫httpclient的工具類

/*** 建立closeablehttpclient

/*** 建立requestconfig

*/private void

createrequestconfig()

/*** get請求不帶引數

*@param

url*

@return

*@throws

exception

*/public string doget(string url) throws exception

return null;

} /**

* get請求帶引數

*@param

url*

@param

map*

@return

*@throws

exception

*/public string doget(string url, map, object> map) throws exception

}return this.doget(uribuilder.build().tostring());

} /**

* 帶引數的post請求

* post請求不帶引數

*@param

url*

@return

*@throws

exception

*/public string dopost(string url) throws exception3. 利用unit進行單元測試

@test

public void

test1() catch (exception e)

可以得到返回的html頁面

4.使用開源框架jsoup進行html頁面的解析

4.1  加入jsoup依賴

org.jsoup

jsoup

1.7.3

4.2 jsoup解析頁面

private void 

parsehtml(string result)

for (element text : textelements)

for (element img : imgelements)

}private void

print(string str , object...msg)

private static string trim(string str, int width) else

}

Android開發實現HttpClient工具類

在android開發中我們經常會用到網路連線功能與伺服器進行資料的互動,為此android的sdk提供了apache的httpclient來方便我們使用各種http服務。你可以把httpclient想象成乙個瀏覽器,通過它的api我們可以很方便的發出get,post請求 當然它的功能遠不止這些 比如...

使用單例模式實現自己的HttpClient工具類

在android開發中我們經常會用到網路連線功能與伺服器進行資料的互動,為此android的sdk提供了apache的httpclient來方便我們使用各種http服務。你可以把httpclient想象成乙個瀏覽器,通過它的api我們可以很方便的發出get,post請求 當然它的功能遠不止這些 比如...

使用單例模式實現自己的HttpClient工具類

在android開發中我們經常會用到網路連線功能與伺服器進行資料的互動,為此android的sdk提供了apache的httpclient來方便我們使用各種http服務。你可以把httpclient想象成乙個瀏覽器,通過它的api我們可以很方便的發出get,post請求 當然它的功能遠不止這些 比如...