<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      httpClient爬蟲

       1 package httpClient.client;
       2 
       3 import java.io.File;
       4 import java.io.IOException;
       5 import java.io.InputStream;
       6 import java.util.UUID;
       7 
       8 import org.apache.commons.io.FileUtils;
       9 import org.apache.http.HttpEntity;
      10 import org.apache.http.client.ClientProtocolException;
      11 import org.apache.http.client.methods.CloseableHttpResponse;
      12 import org.apache.http.client.methods.HttpGet;
      13 import org.apache.http.impl.client.CloseableHttpClient;
      14 import org.apache.http.impl.client.HttpClients;
      15 import org.apache.http.util.EntityUtils;
      16 import org.jsoup.Jsoup;
      17 import org.jsoup.nodes.Document;
      18 import org.jsoup.nodes.Element;
      19 import org.jsoup.select.Elements;
      20 
      21 public class HttpClinet {
      22 
      23     public static void main(String[] args) throws ClientProtocolException, IOException {
      24         // 圖片路徑
      25         String url = "https://www.mzitu.com/";
      26         // 創建httpClient實例
      27         CloseableHttpClient httpClient = HttpClients.createDefault();
      28         HttpClinet t = new HttpClinet();
      29         HttpEntity httpEntity = t.getEntity(httpClient, url);
      30         String html = EntityUtils.toString(httpEntity, "UTF-8");
      31         Document document = Jsoup.parse(html);
      32         // 像js一樣,通過標簽獲取title
      33         // System.out.println(document.getElementsByTag("title").first());
      34         // 像js一樣,通過id 獲取文章列表元素對象
      35         Element postList = document.getElementById("pins");
      36         // 像js一樣,通過class 獲取列表下的所有博客
      37         Elements postItems = postList.select("li a");
      38         // 循環處理每篇博客
      39         String s = "0";
      40         for (Element postItem : postItems) {
      41             String urls = postItem.attr("href").trim();
      42             if (!s.equals(urls)) {
      43                 s = urls;
      44                 HttpEntity httpEntitys = t.getEntity(httpClient, urls);
      45                 String htmls = EntityUtils.toString(httpEntitys, "UTF-8");
      46                 Document documents = Jsoup.parse(htmls);
      47                 String postLists = documents.getElementsByClass("main-image").first().select("p a img").attr("src");
      48                 if (postLists != null) {
      49                     System.out.println(postLists);
      50                     t.save(postLists, httpClient);
      51                 }
      52             }
      53         }
      54         t.close(httpClient);
      55     }
      56 
      57     public void save(String url, CloseableHttpClient httpClient) throws ClientProtocolException, IOException {
      58         String fileName = url.substring(url.lastIndexOf("."), url.length());
      59         HttpEntity entity = this.getEntity(httpClient, url); // 獲取返回實體
      60         if (entity != null) {
      61             System.out.println("Content-Type:" + entity.getContentType().getValue());
      62             InputStream inputStream = entity.getContent();
      63             // 文件復制,common io 包下,需要 引入依賴
      64             FileUtils.copyToFile(inputStream, new File(UUID.randomUUID() + fileName));
      65         }
      66     }
      67 
      68     public void close(CloseableHttpClient httpClient) throws IOException {
      69         if (httpClient != null) {
      70             httpClient.close();
      71         }
      72     }
      73 
      74     public HttpEntity getEntity(CloseableHttpClient httpClient, String url) throws ClientProtocolException, IOException {
      75         HttpGet httpGet = new HttpGet(url);
      76         httpGet.setHeader("If-None-Match", "W/\"5cc2cd8f-2c58");
      77         httpGet.setHeader("Referer", "http://www.mzitu.com/all/");
      78         httpGet.setHeader("User-Agent",
      79                 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36");
      80         CloseableHttpResponse response = httpClient.execute(httpGet);
      81         return response.getEntity();
      82     }
      83 }
      pom.xml

       

      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
      
        <groupId>httpClient</groupId>
        <artifactId>client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
      
        <name>client</name>
        <url>http://maven.apache.org</url>
      
        <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
      
        <dependencies>
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
          </dependency>
          
          <dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
          <version>4.5.8</version>
      </dependency>
      <dependency>
          <groupId>org.jsoup</groupId>
          <artifactId>jsoup</artifactId>
          <version>1.12.1</version>
      </dependency>    
        <dependency>
              <groupId>commons-io</groupId>
              <artifactId>commons-io</artifactId>
              <version>2.5</version>
          </dependency>
        </dependencies>
      </project>

       

      posted @ 2019-11-16 00:02  天葬  閱讀(247)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 日韩精品亚洲不卡一区二区| 成人精品一区日本无码网| 国产久久热这里只有精品| 日本不卡片一区二区三区| 国产精品一区二区AV| 扶余县| 亚洲乱妇老熟女爽到高潮的片| 麻豆国产黄色一级免费片| 狠狠色丁香婷婷综合尤物| 粗了大了 整进去好爽视频| 国产一级片内射在线视频| 亚洲一区二区三级av| 亚洲AV永久无码精品秋霞电影影院| 久久久无码一区二区三区| 亚洲国产精品久久久久秋霞| 91精品人妻中文字幕色| 亚洲av专区一区| 西西人体大胆444WWW| 亚洲国产欧美在线观看片| 国产在线观看免费人成视频| 蓬莱市| 天天综合色天天综合色h| 国产国拍亚洲精品永久软件| 精品久久久久久无码免费| 国产极品美女高潮抽搐免费网站| 国产肥妇一区二区熟女精品| 性欧美vr高清极品| 看亚洲黄色不在线网占| 蜜臀av一区二区三区在线| 蜜臀av在线观看| 亚洲国产中文在线有精品| 国产一区二三区日韩精品| 欧美日本在线| 成人网站免费观看永久视频下载| 日韩精品成人区中文字幕| 国产尤物精品自在拍视频首页| 67194熟妇在线观看线路| 亚洲欧美日韩综合一区二区| 亚洲国产欧美一区二区好看电影| 日韩av无码中文无码电影| 99久久久无码国产麻豆|