詳解Java實(shí)現(xiàn)多種方式的http數(shù)據(jù)抓取
前言:
時(shí)下互聯(lián)網(wǎng)第一波的浪潮已消逝,隨著而來(lái)的基于萬(wàn)千數(shù)據(jù)的物聯(lián)網(wǎng)時(shí)代,因而數(shù)據(jù)成為企業(yè)的重要戰(zhàn)略資源之一?;跀?shù)據(jù)抓取技術(shù),本文介紹了java相關(guān)抓取工具,并附上demo源碼供感興趣的朋友測(cè)試!
1)JDK自帶HTTP連接,獲取頁(yè)面或Json
2) JDK自帶URL連接,獲取頁(yè)面或Json
3)HttpClient Get工具,獲取頁(yè)面或Json
4)commons-io工具,獲取頁(yè)面或Json
5) Jsoup工具(通常用于html字段解析),獲取頁(yè)面,非Json返回格式】
--------------------------------------------------------------------------------
完整代碼:
package com.yeezhao.common.http;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
/**
* http工具對(duì)比
*
* @author Administrator -> junhong
*
* 2016年12月27日
*/
public class HttpFetchUtil {
/**
* 獲取訪問(wèn)的狀態(tài)碼
* @param request
* @return
* @throws Exception
*/
public static int getResponseCode(String request) throws Exception {
URL url = new URL(request);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
return conn.getResponseCode();
}
/**
* 1)JDK自帶HTTP連接,獲取頁(yè)面或Json
* @param request
* @param charset
* @return
* @throws Exception
*/
public static String JDKFetch(String request, String charset) throws Exception {
URL url = new URL(request);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//模擬瀏覽器參數(shù)
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36"
+ " (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream input = conn.getInputStream();
StringBuffer sb = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(input, charset));
String s;
while ((s = reader.readLine()) != null) {
sb.append(s + "\n");
}
input.close();
conn.disconnect();
return sb.toString();
}
return "";
}
/**
* 2) JDK自帶URL連接,獲取頁(yè)面或Json
* @param request
* @param charset
* @return
* @throws Exception
*/
public static String URLFetch(String request, String charset) throws Exception {
URL url = new URL(request);
return IOUtils.toString(url.openStream());
}
/**
* 3)HttpClient Get工具,獲取頁(yè)面或Json
* @param url
* @param charset
* @return
* @throws Exception
*/
public static String httpClientFetch(String url, String charset) throws Exception {
// GET
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset(charset);
HttpMethod method = new GetMethod(url);
httpClient.executeMethod(method);
return method.getResponseBodyAsString();
}
/**
* 4)commons-io工具,獲取頁(yè)面或Json
* @param url
* @param charset
* @return
* @throws Exception
*/
public static String commonsIOFetch(String url, String charset) throws Exception {
return IOUtils.toString(new URL(url), charset);
}
/**
* 5) Jsoup工具(通常用于html字段解析),獲取頁(yè)面,非Json返回格式
* @param url
* @return
* @throws Exception
*/
public static String jsoupFetch(String url) throws Exception {
return Jsoup.parse(new URL(url), 2 * 1000).html();
}
}
測(cè)試代碼:
package com.yeezhao.common.http;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* 測(cè)試類
* 3個(gè)測(cè)試鏈接:
* 1)百科網(wǎng)頁(yè)
* 2)瀏覽器模擬獲取接口數(shù)據(jù)
* 3)獲取普通接口數(shù)據(jù)
* @author Administrator -> junhong
*
* 2016年12月27日
*/
public class HttpFetchUtilTest {
String seeds[] = {"http://baike.baidu.com/view/1.htm","http://m.ximalaya.com/tracks/26096131.json","http://remyapi.yeezhao.com/api/query?wd=%E5%91%A8%E6%98%9F%E9%A9%B0%E7%9A%84%E7%94%B5%E5%BD%B1"};
final static String DEFAULT_CHARSET = "UTF-8";
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
System.out.println("--- down ---");
}
@Test
public void testGetResponseCode() throws Exception{
for(String seed:seeds){
int responseCode = HttpFetchUtil.getResponseCode(seed);
System.out.println("ret="+responseCode);
}
}
@Test
public void testJDKFetch() throws Exception{
for(String seed:seeds){
String ret = HttpFetchUtil.JDKFetch(seed, DEFAULT_CHARSET);
System.out.println("ret="+ret);
}
}
@Test
public void testURLFetch() throws Exception{
for(String seed:seeds){
String ret = HttpFetchUtil.URLFetch(seed, DEFAULT_CHARSET);
System.out.println("ret="+ret);
}
}
@Test
public void testHttpClientFetch()throws Exception {
for(String seed:seeds){
String ret = HttpFetchUtil.httpClientFetch(seed, DEFAULT_CHARSET);
System.out.println("ret="+ret);
}
}
@Test
public void testCommonsIOFetch()throws Exception {
for(String seed:seeds){
String ret = HttpFetchUtil.commonsIOFetch(seed, DEFAULT_CHARSET);
System.out.println("ret="+ret);
}
}
@Test
public void testJsoupFetch() throws Exception{
for(String seed:seeds){
String ret = HttpFetchUtil.jsoupFetch(seed);
System.out.println("ret="+ret);
}
}
}
附:相關(guān)jar依賴
... <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.7.3</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> ...
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用java項(xiàng)目搭建一個(gè)netty服務(wù)
這篇文章主要為大家詳細(xì)介紹了如何使用java項(xiàng)目搭建一個(gè)netty服務(wù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-10-10
Java編寫實(shí)現(xiàn)九宮格應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Java編寫實(shí)現(xiàn)九宮格應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Spring Cloud Gateway Hystrix fallback獲取異常信息的處理
這篇文章主要介紹了Spring Cloud Gateway Hystrix fallback獲取異常信息的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java中綴表達(dá)式轉(zhuǎn)后綴表達(dá)式實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Java中綴表達(dá)式轉(zhuǎn)后綴表達(dá)式實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Java中綴表達(dá)式轉(zhuǎn)換成后綴表達(dá)式的相關(guān)算法原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03
IntelliJ IDEA設(shè)置JVM運(yùn)行參數(shù)的操作方法
這篇文章主要介紹了IntelliJ IDEA設(shè)置JVM運(yùn)行參數(shù)的操作方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03

