《PHP編程:php實現(xiàn)爬取和分析知乎用戶數(shù)據(jù)》要點:
本文介紹了PHP編程:php實現(xiàn)爬取和分析知乎用戶數(shù)據(jù),希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP學(xué)習(xí)配景說明:小拽利用php的curl寫的爬蟲,實驗性的爬取了知乎5w用戶的基本信息;同時,針對爬取的數(shù)據(jù),進行了簡單的分析呈現(xiàn).
PHP學(xué)習(xí)php的spider代碼和用戶dashboard的展現(xiàn)代碼,整理后上傳github,在個人博客和公眾號更新代碼庫,程序僅供娛樂和學(xué)習(xí)交流;如果有侵犯知乎相關(guān)權(quán)益,請盡快聯(lián)系本人刪除.
PHP學(xué)習(xí)無圖無真相
PHP學(xué)習(xí)移動端分析數(shù)據(jù)截圖
PHP學(xué)習(xí)
PHP學(xué)習(xí)pc端分析數(shù)據(jù)截圖
PHP學(xué)習(xí)
PHP學(xué)習(xí)整個爬取,分析,展現(xiàn)過程大概分如下幾步,小拽將分別介紹
PHP學(xué)習(xí)curl爬取網(wǎng)頁數(shù)據(jù)
PHP學(xué)習(xí)PHP的curl擴展是PHP支持的,允許你與各種服務(wù)器使用各種類型的協(xié)議進行連接和通信的庫.是一個非常便捷的抓取網(wǎng)頁的工具,同時,支持多線程擴展.
PHP學(xué)習(xí)本程序抓取的是知乎對外提供用戶拜訪的個人信息頁面https://www.zhihu.com/people/xxx,抓取過程需要攜帶用戶cookie才能獲取頁面.直接上碼
PHP學(xué)習(xí)獲取頁面cookie
PHP學(xué)習(xí)抓取個人中心頁面
PHP學(xué)習(xí)通過curl,攜帶cookie,先抓取本人中心頁面
PHP學(xué)習(xí)
/**
* 通過用戶名抓取個人中心頁面并存儲
*
* @param $username str :用戶名 flag
* @return boolean :成功與否標(biāo)志
*/
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); //設(shè)置哀求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;
}
PHP學(xué)習(xí)正則分析網(wǎng)頁數(shù)據(jù)分析新鏈接,進一步爬取
PHP學(xué)習(xí)對于抓取過來的網(wǎng)頁進行存儲,要想進行進一步的爬取,頁面必須包括有可用于進一步爬取用戶的鏈接
.通過對知乎頁面分析發(fā)現(xiàn):在個人中心頁面中有關(guān)注人和部分點贊人和被關(guān)注人.
如下所示
PHP學(xué)習(xí)ok,這樣子就可以通過本身-》關(guān)注人-》關(guān)注人的關(guān)注人-》...進行不斷爬取.接下來就是通過正則匹配提取該信息
PHP學(xué)習(xí)到此,整個爬蟲過程就可以順利進行了.
如果必要大量的抓取數(shù)據(jù),可以研究下curl_multi
和pcntl
進行多線程的快速抓取,此處不做贅述.
PHP學(xué)習(xí)分析用戶數(shù)據(jù),提供分析
PHP學(xué)習(xí)通過正則可以進一步匹配出更多的該用戶數(shù)據(jù),直接上碼.
PHP學(xué)習(xí)
// 獲取用戶頭像
preg_match('/<img.+src=\"?([^\s]+\.(jpg|gif|bmp|bnp|png))\"?.+>/i', $str, $match_img);
$img_url = $match_img[1];
// 匹配用戶名:
// <span class="name">崔小拽</span>
preg_match('/<span.+class=\"?name\"?>([\x{4e00}-\x{9fa5}]+).+span>/u', $str, $match_name);
$user_name = $match_name[1];
// 匹配用戶簡介
// class bio span 中文
preg_match('/<span.+class=\"?bio\"?.+\>([\x{4e00}-\x{9fa5}]+).+span>/u', $str, $match_title);
$user_title = $match_title[1];
// 匹配性別
//<input type="radio" name="gender" value="1" checked="checked" class="male"/> 男??
// gender value1 ;結(jié)束 中文
preg_match('/<input.+name=\"?gender\"?.+value=\"?1\"?.+([\x{4e00}-\x{9fa5}]+).+\;/u', $str, $match_sex);
$user_sex = $match_sex[1];
// 匹配地區(qū)
//<span class="location item" title="北京">
preg_match('/<span.+class=\"?location.+\"?.+\"([\x{4e00}-\x{9fa5}]+)\">/u', $str, $match_city);
$user_city = $match_city[1];
// 匹配工作
//<span class="employment item" title="人見人罵的公司">人見人罵的公司</span>
preg_match('/<span.+class=\"?employment.+\"?.+\"([\x{4e00}-\x{9fa5}]+)\">/u', $str, $match_employment);
$user_employ = $match_employment[1];
// 匹配職位
// <span class="position item" title="程序猿"><a href="/topic/19590046" title="程序猿" class="topic-link" data-token="19590046" data-topicid="13253">程序猿</a></span>
preg_match('/<span.+class=\"?position.+\"?.+\"([\x{4e00}-\x{9fa5}]+).+\">/u', $str, $match_position);
$user_position = $match_position[1];
// 匹配學(xué)歷
// <span class="education item" title="研究僧">研究僧</span>
preg_match('/<span.+class=\"?education.+\"?.+\"([\x{4e00}-\x{9fa5}]+)\">/u', $str, $match_education);
$user_education = $match_education[1];
// 工作情況
// <span class="education-extra item" title='挨踢'>挨踢</span>
preg_match('/<span.+class=\"?education-extra.+\"?.+>([\x{4e00}-
\x{9fa5}]+)</u', $str, $match_education_extra);
$user_education_extra = $match_education_extra[1];
// 匹配關(guān)注話題數(shù)量
// class="zg-link-litblue"><strong>41 個話題</strong></a>
preg_match('/class=\"?zg-link-litblue\"?><strong>(\d+)\s.+strong>/i', $str, $match_topic);
$user_topic = $match_topic[1];
// 關(guān)注人數(shù)
// <span class="zg-gray-normal">關(guān)注了
preg_match_all('/<strong>(\d+)<.+<label>/i', $str, $match_care);
$user_care = $match_care[1][0];
$user_be_careed = $match_care[1][1];
// 歷史瀏覽量
// <span class="zg-gray-normal">個人主頁被 <strong>17</strong> 人瀏覽</span>
preg_match('/class=\"?zg-gray-normal\"?.+>(\d+)<.+span>/i', $str, $match_browse);
$user_browse = $match_browse[1];
PHP學(xué)習(xí)在抓取的過程中,有條件的話,必定要通過redis入庫,確實能提升抓取和入庫效率.沒有條件的話只能通過sql優(yōu)化.這里來幾發(fā)心德.
PHP學(xué)習(xí)數(shù)據(jù)庫表設(shè)計索引一定要慎重.在spider爬取的過程中,建議出了用戶名,左右字段都不要索引,包含主鍵都不要,盡可能的提高入庫效率
,試想5000w的數(shù)據(jù),每次添加一個,建立索引需要多少消耗.等抓取完畢,需要分析數(shù)據(jù)時,批量建立索引.
PHP學(xué)習(xí)數(shù)據(jù)入庫和更新操作,必定要批量. mysql 官方給出的增刪改的建議和速度:http://dev.mysql.com/doc/refman/5.7/en/insert-speed.html
PHP學(xué)習(xí)
# 官方的最優(yōu)批量插入
INSERT INTO yourtable VALUES (1,2), (5,5), ...;
PHP學(xué)習(xí)部署操作.程序在抓取過程中,有可能會出現(xiàn)異常掛掉,為了保證高效穩(wěn)定,盡可能的寫一個定時腳本.每隔一段時間干掉,重新跑,這樣即使異常掛掉也不會浪費太多名貴時間,畢竟,time is money.
PHP學(xué)習(xí)
#!/bin/bash
# 干掉
ps aux |grep spider |awk '{print $2}'|xargs kill -9
sleep 5s
# 重新跑
nohup /home/cuixiaohuan/lamp/php5/bin/php /home/cuixiaohuan/php/zhihu_spider/spider_new.php &
PHP學(xué)習(xí)數(shù)據(jù)分析呈現(xiàn)
PHP學(xué)習(xí)數(shù)據(jù)的呈現(xiàn)主要使用echarts 3.0,感覺對于移動端兼容還不錯.兼容移動端的頁面響應(yīng)式結(jié)構(gòu)主要通過幾個簡單的css控制,代碼如下
PHP學(xué)習(xí)
// 獲取用戶頭像
preg_match('/<img.+src=\"?([^\s]+\.(jpg|gif|bmp|bnp|png))\"?.+>/i', $str, $match_img);
$img_url = $match_img[1];
// 匹配用戶名:
// <span class="name">崔小拽</span>
preg_match('/<span.+class=\"?name\"?>([\x{4e00}-\x{9fa5}]+).+span>/u', $str, $match_name);
$user_name = $match_name[1];
// 匹配用戶簡介
// class bio span 中文
preg_match('/<span.+class=\"?bio\"?.+\>([\x{4e00}-\x{9fa5}]+).+span>/u', $str, $match_title);
$user_title = $match_title[1];
// 匹配性別
//<input type="radio" name="gender" value="1" checked="checked" class="male"/> 男??
// gender value1 ;結(jié)束 中文
preg_match('/<input.+name=\"?gender\"?.+value=\"?1\"?.+([\x{4e00}-\x{9fa5}]+).+\;/u', $str, $match_sex);
$user_sex = $match_sex[1];
// 匹配地區(qū)
//<span class="location item" title="北京">
preg_match('/<span.+class=\"?location.+\"?.+\"([\x{4e00}-\x{9fa5}]+)\">/u', $str, $match_city);
$user_city = $match_city[1];
// 匹配工作
//<span class="employment item" title="人見人罵的公司">人見人罵的公司</span>
preg_match('/<span.+class=\"?employment.+\"?.+\"([\x{4e00}-\x{9fa5}]+)\">/u', $str, $match_employment);
$user_employ = $match_employment[1];
// 匹配職位
// <span class="position item" title="程序猿"><a href="/topic/19590046" title="程序猿" class="topic-link" data-token="19590046" data-topicid="13253">程序猿</a></span>
preg_match('/<span.+class=\"?position.+\"?.+\"([\x{4e00}-\x{9fa5}]+).+\">/u', $str, $match_position);
$user_position = $match_position[1];
// 匹配學(xué)歷
// <span class="education item" title="研究僧">研究僧</span>
preg_match('/<span.+class=\"?education.+\"?.+\"([\x{4e00}-\x{9fa5}]+)\">/u', $str, $match_education);
$user_education = $match_education[1];
// 工作情況
// <span class="education-extra item" title='挨踢'>挨踢</span>
preg_match('/<span.+class=\"?education-extra.+\"?.+>([\x{4e00}-
\x{9fa5}]+)</u', $str, $match_education_extra);
$user_education_extra = $match_education_extra[1];
// 匹配關(guān)注話題數(shù)量
// class="zg-link-litblue"><strong>41 個話題</strong></a>
preg_match('/class=\"?zg-link-litblue\"?><strong>(\d+)\s.+strong>/i', $str, $match_topic);
$user_topic = $match_topic[1];
// 關(guān)注人數(shù)
// <span class="zg-gray-normal">關(guān)注了
preg_match_all('/<strong>(\d+)<.+<label>/i', $str, $match_care);
$user_care = $match_care[1][0];
$user_be_careed = $match_care[1][1];
// 歷史瀏覽量
// <span class="zg-gray-normal">個人主頁被 <strong>17</strong> 人瀏覽</span>
preg_match('/class=\"?zg-gray-normal\"?.+>(\d+)<.+span>/i', $str, $match_browse);
$user_browse = $match_browse[1];
PHP學(xué)習(xí)不敷和待學(xué)習(xí)
PHP學(xué)習(xí)整個過程中涉及php,shell,js,css,html,正則等語言和部署等基礎(chǔ)知識,但還有諸多必要改進完善,小拽特此記錄,后續(xù)補充例:
歡迎參與《PHP編程:php實現(xiàn)爬取和分析知乎用戶數(shù)據(jù)》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/7621.html