1. 程式人生 > 其它 >MinIO Python Client SDK 快速入門指南

MinIO Python Client SDK 快速入門指南

網路爬蟲就是用程式幫助我們訪問網路上的資源,我們一直以來都是使用HTTP協議訪問網際網路的網頁,網路爬蟲需要編寫程式,在這裡使用同樣的 HTTP協議訪問網頁。

加入依賴:

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.2</version>
</dependency>

配置log4j.properties檔案

log4j.rootLogger = DEBUG,A1
log4j.logger.org.example01 
= DEBUG 1og4j.appender.A1 = org.apache.log4j.ConsoleAppender log4j.appender.A1.layout = org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss,SSS) [%c] [%c]-[%p] %m%n

以GET請求顯示爬蟲步驟:

public static void main( String[] args ) {
        //1.建立HttpClient物件
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.輸入網址,發起get請求建立HttpGet物件
        HttpGet httpGet = new HttpGet("http://baojia.steelcn.cn/");
        //3.使用httpClient物件發起請求
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            //4.解析相應,獲取資料
            //判斷狀態碼是否相應
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = response.getEntity();
                String content = EntityUtils.toString(httpEntity, "utf-8");
                System.out.println(content.length());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
//            關閉釋放資源
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //關閉瀏覽器
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

  URL是http協議顯示

帶get帶參請求:

//設定請求地址是:http://www.itcast.com/search?keys=java
//建立URIBuilder
URIBuilder uriBuilder = new URIBuilder("http://www.itcast.com/search?keys=java");
//設定引數
uriBuilder.setParameter("keys","java");
//2.輸入網址,發起get請求建立HttpGet物件
HttpGet httpGet = new HttpGet(uriBuilder.build());
System.out.println(
"發起請求的資訊"+httpGet);

多個引數是可以繼續uriBuilder.setParameter("keys","java").setParameter(param,value)

對與pose請求與get類似

設定爬蟲引數:連線時間,傳輸時間

//配置請求資訊
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000)//建立連結的最長時間,單位是毫秒
        .setConnectionRequestTimeout(500)//設定獲取連結的最長時間,單位毫秒
        .setSocketTimeout(10 * 1000)//設定資料傳輸最長時間,單位毫秒
        .build();
//給請求設定請求資訊
httpGet.setConfig(config);