php實現爬取和分析知乎使用者資料,php_PHP教程

來源:互聯網
上載者:User

php實現爬取和分析知乎使用者資料,php


背景說明:小拽利用php的curl寫的爬蟲,實驗性的爬取了知乎5w使用者的基本資料;同時,針對爬取的資料,進行了簡單的分析呈現。

php的spider代碼和使用者dashboard的展現代碼,整理後上傳github,在個人部落格和公眾號更新程式碼程式庫,程式僅供娛樂和學習交流;如果有侵犯知乎相關權益,請儘快聯絡本人刪除。

無圖無真相

移動端分析資料

pc端分析資料

整個爬取,分析,展現過程大概分如下幾步,小拽將分別介紹

  1. curl爬取知乎網頁資料
  2. 正則分析知乎網頁資料
  3. 資料資料入庫和程式部署
  4. 資料分析和呈現

curl爬取網頁資料

PHP的curl擴充是PHP支援的,允許你與各種伺服器使用各種類型的協議進行串連和通訊的庫。是一個非常便捷的抓取網頁的工具,同時,支援多線程擴充。

本程式抓取的是知乎對外提供使用者訪問的個人資訊頁面https://www.zhihu.com/people/xxx,抓取過程需要攜帶使用者cookie才能擷取頁面。直接上碼

擷取頁面cookie

複製代碼 代碼如下:
// 登入知乎,開啟個人中心,開啟控制台,擷取cookie
document.cookie
"_za=67254197-3wwb8d-43f6-94f0-fb0e2d521c31; _ga=GA1.2.2142818188.1433767929; q_c1=78ee1604225d47d08cddd8142a08288b23|1452172601000|1452172601000; _xsrf=15f0639cbe6fb607560c075269064393; cap_id="N2QwMTExNGQ0YTY2NGVddlMGIyNmQ4NjdjOTU0YTM5MmQ=|1453444256|49fdc6b43dc51f702b7d6575451e228f56cdaf5d"; __utmt=1; unlock_ticket="QUJDTWpmM0lsZdd2dYQUFBQVlRSlZUVTNVb1ZaNDVoQXJlblVmWGJ0WGwyaHlDdVdscXdZU1VRPT0=|1453444421|c47a2afde1ff334d416bafb1cc267b41014c9d5f"; __utma=51854390.21428dd18188.1433767929.1453187421.1453444257.3; __utmb=51854390.14.8.1453444425011; __utmc=51854390; __utmz=51854390.1452846679.1.dd1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __utmv=51854390.100-1|2=registration_date=20150823=1^dd3=entry_date=20150823=1"

抓取個人中心頁面

通過curl,攜帶cookie,先抓取本人中心頁面

/** * 通過使用者名稱抓取個人中心頁面並儲存 *  * @param $username str :使用者名稱 flag * @return boolean   :成功與否標誌 */public function spiderUser($username){  $cookie = "xxxx" ;  $url_info = 'http://www.zhihu.com/people/' . $username; //此處cui-xiao-zhuai代表使用者ID,可以直接看url擷取本人id  $ch = curl_init($url_info); //初始化會話  curl_setopt($ch, CURLOPT_HEADER, 0);  curl_setopt($ch, CURLOPT_COOKIE, $cookie); //佈建要求COOKIE  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //將curl_exec()擷取的資訊以檔案流的形式返回,而不是直接輸出。  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  $result = curl_exec($ch);   file_put_contents('/home/work/zxdata_ch/php/zhihu_spider/file/'.$username.'.html',$result);   return true; }

正則分析網頁資料分析新連結,進一步爬取

對於抓取過來的網頁進行儲存,要想進行進一步的爬取,頁面必須包含有可用於進一步爬取使用者的連結。通過對知乎頁面分析發現:在個人中心頁面中有關注人和部分點贊人和被關注人。
如下所示

複製代碼 代碼如下:
// 抓取的html頁面中發現了新的使用者,可用於爬蟲

ok,這樣子就可以通過自己-》關注人-》關注人的關注人-》。。。進行不斷爬取。接下來就是通過正則匹配提取該資訊

複製代碼 代碼如下:
// 匹配到抓取頁面的所有使用者
preg_match_all('/\/people\/([\w-]+)\"/i', $str, $match_arr);
// 去重合并入新的使用者數組,使用者進一步抓取
self::$newUserArr = array_unique(array_merge($match_arr[1], self::$newUserArr));

到此,整個爬蟲過程就可以順利進行了。
如果需要大量的抓取資料,可以研究下curl_multipcntl進行多線程的快速抓取,此處不做贅述。

分析使用者資料,提供分析

通過正則可以進一步匹配出更多的該使用者資料,直接上碼。

// 擷取帳戶圖片preg_match('//i', $str, $match_img);$img_url = $match_img[1];// 匹配使用者名稱:// 崔小拽preg_match('/([\x{4e00}-\x{9fa5}]+).+span>/u', $str, $match_name);$user_name = $match_name[1];// 匹配使用者簡介// class bio span 中文preg_match('/([\x{4e00}-\x{9fa5}]+).+span>/u', $str, $match_title);$user_title = $match_title[1];// 匹配性別// 男  // gender value1 ;結束 中文preg_match('/preg_match('//u', $str, $match_city);$user_city = $match_city[1];// 匹配工作//人見人罵的公司preg_match('//u', $str, $match_employment);$user_employ = $match_employment[1];// 匹配職位// 程式猿preg_match('//u', $str, $match_position);$user_position = $match_position[1];// 匹配學曆// 研究僧preg_match('//u', $str, $match_education);$user_education = $match_education[1];// 工作情況// 挨踢preg_match('/([\x{4e00}-\x{9fa5}]+)41 個話題preg_match('/class=\"?zg-link-litblue\"?>(\d+)\s.+strong>/i', $str, $match_topic);$user_topic = $match_topic[1];// 關注人數// 關注了preg_match_all('/(\d+)<.+/i', $str, $match_care);$user_care = $match_care[1][0];$user_be_careed = $match_care[1][1];// 曆史瀏覽量// 個人首頁被 17 人瀏覽preg_match('/class=\"?zg-gray-normal\"?.+>(\d+)<.+span>/i', $str, $match_browse);$user_browse = $match_browse[1];

在抓取的過程中,有條件的話,一定要通過redis入庫,確實能提升抓取和入庫效率。沒有條件的話只能通過sql最佳化。這裡來幾發心德。

資料庫表設計索引一定要謹慎。在spider爬取的過程中,建議出了使用者名稱,左右欄位都不要索引,包括主鍵都不要,儘可能的提高入庫效率,試想5000w的資料,每次添加一個,建立索引需要多少消耗。等抓取完畢,需要分析資料時,批量建立索引。

資料入庫和更新操作,一定要批量。 mysql 官方給出的增刪改的建議和速度:http://dev.mysql.com/doc/refman/5.7/en/insert-speed.html

# 官方的最優批量插入INSERT INTO yourtable VALUES (1,2), (5,5), ...;

部署操作。程式在抓取過程中,有可能會出現異常掛掉,為了保證高效穩定,儘可能的寫一個定時指令碼。每隔一段時間幹掉,重新跑,這樣即使異常掛掉也不會浪費太多寶貴時間,畢竟,time is money。

#!/bin/bash# 幹掉ps aux |grep spider |awk '{print $2}'|xargs kill -9sleep 5s# 重新跑nohup /home/cuixiaohuan/lamp/php5/bin/php /home/cuixiaohuan/php/zhihu_spider/spider_new.php & 

資料分析呈現

資料的呈現主要使用echarts 3.0,感覺對於移動端相容還不錯。相容移動端的頁面響應式布局主要通過幾個簡單的css控制,代碼如下

// 擷取帳戶圖片preg_match('//i', $str, $match_img);$img_url = $match_img[1];// 匹配使用者名稱:// 崔小拽preg_match('/([\x{4e00}-\x{9fa5}]+).+span>/u', $str, $match_name);$user_name = $match_name[1];// 匹配使用者簡介// class bio span 中文preg_match('/([\x{4e00}-\x{9fa5}]+).+span>/u', $str, $match_title);$user_title = $match_title[1];// 匹配性別// 男 // gender value1 ;結束 中文preg_match('/preg_match('//u', $str, $match_city);$user_city = $match_city[1];// 匹配工作//人見人罵的公司preg_match('//u', $str, $match_employment);$user_employ = $match_employment[1];// 匹配職位// 程式猿preg_match('//u', $str, $match_position);$user_position = $match_position[1];// 匹配學曆// 研究僧preg_match('//u', $str, $match_education);$user_education = $match_education[1];// 工作情況// 挨踢preg_match('/([\x{4e00}-\x{9fa5}]+)41 個話題preg_match('/class=\"?zg-link-litblue\"?>(\d+)\s.+strong>/i', $str, $match_topic);$user_topic = $match_topic[1];// 關注人數// 關注了preg_match_all('/(\d+)<.+/i', $str, $match_care);$user_care = $match_care[1][0];$user_be_careed = $match_care[1][1];// 曆史瀏覽量// 個人首頁被 17 人瀏覽preg_match('/class=\"?zg-gray-normal\"?.+>(\d+)<.+span>/i', $str, $match_browse);$user_browse = $match_browse[1];

不足和待學習

整個過程中涉及php,shell,js,css,html,正則等語言和部署等基礎知識,但還有諸多需要改進完善,小拽特此記錄,後續補充例:

  1. php 採用multicul進行多線程。
  2. 正則匹配進一步最佳化
  3. 部署和抓取過程採用redis提升儲存
  4. 移動端布局的相容性提升
  5. js的模組化和sass書寫css。

您可能感興趣的文章:

  • php IIS日誌分析搜尋引擎爬蟲記錄程式
  • php 向訪客和爬蟲顯示不同的內容
  • 一個PHP實現的輕量級簡單爬蟲
  • PHP實現簡單爬蟲的方法
  • PHP代碼實現爬蟲記錄——超管用
  • PHP爬蟲之百萬層級知乎使用者資料爬取與分析

http://www.bkjia.com/PHPjc/1096148.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1096148.htmlTechArticlephp實現爬取和分析知乎使用者資料,php 背景說明:小拽利用php的curl寫的爬蟲,實驗性的爬取了知乎5w使用者的基本資料;同時,針對爬取的資料...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.