生活随笔
收集整理的這篇文章主要介紹了
使用java爬取数据的三种思路
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
- 一、使用HttpClient
- 二、使用HtmlUnit
- 三、捕獲接口獲取數(shù)據(jù)
一、使用HttpClient
HttpClient是一種簡(jiǎn)單的捕獲html頁(yè)面的工具包,現(xiàn)已不再維護(hù),已經(jīng)被Apache 的HttpComponents替代了,缺陷就是獲取不到j(luò)s獲取到的動(dòng)態(tài)數(shù)據(jù),從而使得爬取的數(shù)據(jù)缺失。
<dependency><groupId>org
.apache
.httpcomponents
</groupId
><artifactId>httpclient
</artifactId
><version>4.5.8</version
>
</dependency
>
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;public class HttpClientTest {public static String getHtmlByHttpClient(String url
) {CloseableHttpClient httpClient
= HttpClients.createDefault();CloseableHttpResponse response
= null;HttpGet request
= new HttpGet(url
);try {response
= httpClient
.execute(request
);if(response
.getStatusLine().getStatusCode() == HttpStatus.SC_OK
) {HttpEntity httpEntity
= response
.getEntity();String html
= EntityUtils.toString(httpEntity
, "utf-8");return html
;} else {System.out
.println("返回狀態(tài)不是200");System.out
.println(EntityUtils.toString(response
.getEntity(), "utf-8"));}} catch (ClientProtocolException e
) {e
.printStackTrace();} catch (IOException e
) {e
.printStackTrace();} finally {HttpClientUtils.closeQuietly(response
);HttpClientUtils.closeQuietly(httpClient
);}}
}
二、使用HtmlUnit
HtmlUnit可以用來(lái)模擬瀏覽器運(yùn)行,可以把它當(dāng)作一個(gè)沒(méi)有界面的瀏覽器,也就是用代碼模擬鼠標(biāo)等操作來(lái)操作網(wǎng)頁(yè),運(yùn)行速度快。
HtmlUnit是一款開(kāi)源的java 頁(yè)面分析工具,作為junit的擴(kuò)展之一,可以模擬js運(yùn)行
->使用htmlUnit捕獲百度搜索頁(yè)面
通過(guò)htmlUnit操作百度高級(jí)搜索界面最終捕獲搜索結(jié)果的html頁(yè)面內(nèi)容
<dependency> <groupId>net
.sourceforge
.htmlunit
</groupId
> <artifactId>htmlunit
</artifactId
> <version>2.23</version
>
</dependency
>
public static String Baidu(String keyword
)throws Exception{WebClient webclient
= new WebClient();webclient
.getOptions().setThrowExceptionOnScriptError(false);webclient
.getOptions().setThrowExceptionOnFailingStatusCode(false);webclient
.getOptions().setCssEnabled(false);webclient
.getOptions().setJavaScriptEnabled(true);HtmlPage htmlpage
= webclient
.getPage("http://www.baidu.com/gaoji/advanced.html");HtmlForm form
= htmlpage
.getFormByName("f1");HtmlSubmitInput button
= form
.getInputByValue("百度一下");HtmlTextInput textField
= form
.getInputByName("q1");textField
.setValueAttribute(keyword
);final HtmlSelect htmlSelet
=form
.getSelectByName("rn");htmlSelet
.setDefaultValue("10");final HtmlHiddenInput hiddenInputtn
= form
.getInputByName("tn");hiddenInputtn
.setDefaultValue("baiduadv");final HtmlPage page
= button
.click();String result
= page
.asText();webclient
.close();return result
;
}
三、捕獲接口獲取數(shù)據(jù)
通過(guò)前兩個(gè)方法往往有時(shí)候可能無(wú)法得到我們想要的結(jié)果,捕獲的html頁(yè)面代碼可能會(huì)缺失數(shù)據(jù),同時(shí)還有可能會(huì)被網(wǎng)站監(jiān)測(cè)到進(jìn)而被禁掉ip地址,導(dǎo)致我們無(wú)法繼續(xù)獲取頁(yè)面數(shù)據(jù)
我們想想數(shù)據(jù)是從哪來(lái)的,一般都是通過(guò)接口得到的對(duì)吧,那么要是我們可以直接訪問(wèn)接口呢?下面是我的思路:
- 1、找我們想要爬取數(shù)據(jù)的網(wǎng)站,通過(guò)在對(duì)應(yīng)網(wǎng)站按下F12調(diào)出控制臺(tái),然后我們刷新一下頁(yè)面,在NetWork板塊查看請(qǐng)求數(shù)據(jù)
如圖查看XHR這一欄可以看訪問(wèn)的url - 2、這里我們用csdn博客舉例,在XHR這一欄尋找那些響應(yīng)格式為json的url,這里我們找到了一條:
- 3、我們使用postman測(cè)試一下該接口會(huì)返回什么數(shù)據(jù):
通過(guò)文字我們可以知道,這里的數(shù)據(jù)是導(dǎo)航欄這里的,如下
因此我們可以通過(guò)調(diào)用該接口獲取到對(duì)應(yīng)數(shù)據(jù)啦!最后將數(shù)據(jù)封裝成為我們需要的格式就ok。
這種方法雖然可以穩(wěn)定獲取到數(shù)據(jù),但實(shí)際上在很多網(wǎng)站上我們?cè)赬HR請(qǐng)求中是看不到接口的,這是由于考慮了跨域而使用了jsonp,這些可以在js中找,感興趣的可以了解一下
總結(jié)
以上是生活随笔為你收集整理的使用java爬取数据的三种思路的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。