基於apache —HttpClient的小爬蟲擷取網頁內容

來源:互聯網
上載者:User

標籤:default   blank   實現   oid   lan   網頁   system   get請求   created   

  今天(17-03-31)忙了一下午研究webmagic,發現自己還太年輕,對於這樣難度的架構(類庫)

還是難以接受,還是從基礎開始吧,因為相對基礎的東西教程相多一些,於是乎我找了apache其下的

HttpClient,根據前輩們發的教程自己也簡單寫了一下,感覺還好。

 

下面實現的是單個頁面的擷取:

import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import java.io.IOException;public class Main {    public static void main(String[] args) throws IOException{        try {            //建立client執行個體            HttpClient client= HttpClients.createDefault();            //建立httpget執行個體            HttpGet httpGet=new HttpGet("http://www.btba.com.cn");            //執行 get請求            HttpResponse response=client.execute(httpGet);            //返回擷取實體            HttpEntity entity=response.getEntity();            //擷取網頁內容,指定編碼            String web= EntityUtils.toString(entity,"UTF-8");            //輸出網頁            System.out.println(web);        } catch (IOException e) {            e.printStackTrace();        }    }}

部分展示:

 

下面提供了HttpClient的下載: http://hc.apache.org/downloads.cgi

 

webmagic基於HttpClient、Jsoup 所以,現將這兩個學會了、學懂了在去嘗試啃webmagic吧

下一篇講Jsoup對於本篇下載網頁的簡單解析處理。。。

 

本人還是小白一隻,以上有什麼不足或者不對之處請指出,非常感謝個位。

 

基於apache —HttpClient的小爬蟲擷取網頁內容

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.