<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          我用 Java 爬了一下CSDN,發(fā)現(xiàn)了這些秘密。

          共 6471字,需瀏覽 13分鐘

           ·

          2021-09-03 23:46

          大家好,我是大堯。

          今天我們使用Java語言寫一個(gè)爬蟲,用來爬取csdn首頁推薦博客的博主,看看這些博主有多少人在寫微信公眾號(hào)。

          一、爬蟲原理

          爬蟲就是去請(qǐng)求某個(gè)url,然后將響應(yīng)的頁面進(jìn)行解析,將解析到的數(shù)據(jù)保存,同時(shí)解析出當(dāng)前頁面的url,繼續(xù)進(jìn)行爬取,一直循環(huán)下去,爬取當(dāng)前網(wǎng)站的內(nèi)容。

          二、分析CSDN頁面數(shù)據(jù)

          因?yàn)槲覀兊哪繕?biāo)很明確,就是去分析首頁推薦博客博主寫微信公眾號(hào)的比例,因此我們只需要找到我們需要的數(shù)據(jù)進(jìn)行保存即可,不需要爬取網(wǎng)站的全部數(shù)據(jù)。

          2.1 找到CSDN首頁的博客鏈接

          • 在瀏覽器輸入csdn首頁鏈接https://www.csdn.net/,

          • 找到我們爬取的目標(biāo),如下圖所示

          • 使用f12查看目標(biāo)元素

          根據(jù)目標(biāo)元素,我們可以提取兩個(gè)關(guān)鍵元素,其一是目標(biāo)鏈接在<a></a>中,其二是博客地址的格式為https://blog.csdn.net/+"用戶名"+/article/details/+"文章標(biāo)識(shí)"(記住這個(gè)博客地址,后面有用)。

          2.2 提取設(shè)置了公眾號(hào)信息的博主

          在文章詳情頁面有博主相關(guān)的信息,csdn博客左側(cè)有一塊是博主用來自定義信息的,如下圖:


          還是一樣,f12來查看DOM元素,發(fā)這一塊內(nèi)容在id=asideCustom

          中。

          2.3 爬取思路

          1. 通過爬取首頁,解析出所有a標(biāo)簽

          2. 篩選a標(biāo)簽,根據(jù)博客地址格式,匹配到所有的博客地址

          3. 爬取博客地址,解析id=asideCustom

          4. 如果第3步可以解析出來,則說明該博主設(shè)置了自定義信息

          三、編寫爬蟲

          根據(jù)上面的分析我們需要兩個(gè)工具包,一個(gè)是httpclient用于網(wǎng)絡(luò)請(qǐng)求,另一個(gè)是用來解析DOM元素的jsoup。

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

          <!-- 添加jsoup支持 -->
          <dependency>
              <groupId>org.jsoup</groupId>
              <artifactId>jsoup</artifactId>
              <version>1.10.1</version>
          </dependency>

          網(wǎng)絡(luò)調(diào)用偽代碼

          public static ArrayList<Document> HttpUtil(HashSet<String> urls){
              CloseableHttpClient httpClient = HttpClients.createDefault();
              CloseableHttpResponse response = null;
              ArrayList<Document> list = new ArrayList<>();
              try {
                  for(String url : urls){
                      HttpGet request = new HttpGet(url);
                      response = httpClient.execute(request);

                      //判斷響應(yīng)狀態(tài)為200,請(qǐng)求成功,進(jìn)行處理
                      if(response.getStatusLine().getStatusCode() == 200) {
                          HttpEntity httpEntity = response.getEntity();
                          String html = EntityUtils.toString(httpEntity, "utf-8");
                          Document document = Jsoup.parse(html);
                          list.add(document);
                      } else {
                          System.out.println("返回狀態(tài)不是200");
                      }
                  }
              } catch (ClientProtocolException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                  HttpClientUtils.closeQuietly(response);
                  HttpClientUtils.closeQuietly(httpClient);
              }
              return list;
          }

          調(diào)用及解析偽代碼

          public static void main(String[] args) {

              // 標(biāo)記有多少博主設(shè)置了自定義信息
              int i = 0;
              // 首頁url
              HashSet<String> url = new HashSet<>();
              // 文章urls
              HashSet<String> articleUrls = new HashSet<>();
              url.add("https://www.csdn.net/");
              // 爬取csdn首頁
              ArrayList<Document> list = HttpUtil(url);
              // 選擇a標(biāo)簽
              for(Document document : list){
                  Elements a = document.select("a");
                  for(Element element : a){
                      // 獲取a中的url
                      // <a href = "https://xxxx"> </a>
                      String href = element.attr("href");
                      // 篩選博客地址
                      if(href.contains("article/details")){
                          articleUrls.add(href);
                      }
                  }
              }
              ArrayList<Document> list2 = HttpUtil(articleUrls);
              for(Document document : list2){
                  Element asideCustom = document.getElementById("asideCustom");
                  if(asideCustom != null){
                      i++;
                  }
              }
              // 輸出爬取的文章數(shù)量 和 設(shè)置了自定義信息的博主數(shù)量
              System.out.println("爬取的文章數(shù)量="+articleUrls.size()+"\n"+"寫公眾號(hào)的博主數(shù)量="+i);
          }

          控制臺(tái)輸出信息

          爬取的文章數(shù)量=25
          寫公眾號(hào)的博主數(shù)量=5

          四、結(jié)尾

          從上面的結(jié)果中可以看出,在25篇博客中,就有五個(gè)博主在寫公眾號(hào)。但是,這個(gè)數(shù)據(jù)并不能說明csdn的1/5博主就在更新自己的公眾號(hào)。

          1. csdn首頁推薦數(shù)據(jù)是分頁拉取的,爬蟲只能爬取到第一頁的數(shù)據(jù),也就是25條
          2. 有些博主雖然設(shè)置了自定義信息,但是并不是公眾號(hào)
          3. 有些博主雖然沒有設(shè)置自定義信息,但是在簡(jiǎn)介或者其他地方留了公眾號(hào)名稱

          不過這些都沒關(guān)系,本文的重點(diǎn)是使用java語言寫個(gè)爬蟲程序,來爬取目標(biāo)數(shù)據(jù)。

          贊賞顯得見外,在看才是真愛!

          瀏覽 56
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一区二区三区四区五区六区久久 | 亚洲高清视频在线播放 | 一区二区三区四区免费 | 亚洲无码av观看 亚洲无码福利视频 | 国内精品一区二区 |