麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > PHP > 正文

php實現(xiàn)爬取和分析知乎用戶數(shù)據(jù)

2024-05-04 23:42:35
字體:
供稿:網(wǎng)友
本文給大家介紹的是利用php的curl編寫的爬取知乎用戶數(shù)據(jù)的爬蟲,并分析用戶的各種屬性,有需要的小伙伴可以參考下
 

背景說明:小拽利用php的curl寫的爬蟲,實驗性的爬取了知乎5w用戶的基本信息;同時,針對爬取的數(shù)據(jù),進行了簡單的分析呈現(xiàn)。

php的spider代碼和用戶dashboard的展現(xiàn)代碼,整理后上傳github,在個人博客和公眾號更新代碼庫,程序僅供娛樂和學(xué)習(xí)交流;如果有侵犯知乎相關(guān)權(quán)益,請盡快聯(lián)系本人刪除。

無圖無真相

移動端分析數(shù)據(jù)截圖

php實現(xiàn)爬取和分析知乎用戶數(shù)據(jù)

pc端分析數(shù)據(jù)截圖

php實現(xiàn)爬取和分析知乎用戶數(shù)據(jù)

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

  1. curl爬取知乎網(wǎng)頁數(shù)據(jù)
  2. 正則分析知乎網(wǎng)頁數(shù)據(jù)
  3. 數(shù)據(jù)數(shù)據(jù)入庫和程序部署
  4. 數(shù)據(jù)分析和呈現(xiàn)

curl爬取網(wǎng)頁數(shù)據(jù)

PHP的curl擴展是PHP支持的,允許你與各種服務(wù)器使用各種類型的協(xié)議進行連接和通信的庫。是一個非常便捷的抓取網(wǎng)頁的工具,同時,支持多線程擴展。

本程序抓取的是知乎對外提供用戶訪問的個人信息頁面https://www.zhihu.com/people/xxx,抓取過程需要攜帶用戶cookie才能獲取頁面。直接上碼

獲取頁面cookie

 

復(fù)制代碼代碼如下:

// 登錄知乎,打開個人中心,打開控制臺,獲取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   :成功與否標(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; }

正則分析網(wǎng)頁數(shù)據(jù)分析新鏈接,進一步爬取

對于抓取過來的網(wǎng)頁進行存儲,要想進行進一步的爬取,頁面必須包含有可用于進一步爬取用戶的鏈接。通過對知乎頁面分析發(fā)現(xiàn):在個人中心頁面中有關(guān)注人和部分點贊人和被關(guān)注人。
如下所示

 

復(fù)制代碼代碼如下:

// 抓取的html頁面中發(fā)現(xiàn)了新的用戶,可用于爬蟲
<a class="zm-item-link-avatar avatar-link" href="/people/new-user" data-tip="p$t$new-user">

 

ok,這樣子就可以通過自己-》關(guān)注人-》關(guān)注人的關(guān)注人-》。。。進行不斷爬取。接下來就是通過正則匹配提取該信息

 

復(fù)制代碼代碼如下:

// 匹配到抓取頁面的所有用戶
preg_match_all('///people//([/w-]+)/"/i', $str, $match_arr);
// 去重合并入新的用戶數(shù)組,用戶進一步抓取
self::$newUserArr = array_unique(array_merge($match_arr[1], self::$newUserArr));

 

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

分析用戶數(shù)據(jù),提供分析

通過正則可以進一步匹配出更多的該用戶數(shù)據(jù),直接上碼。

// 獲取用戶頭像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" style="margin: 0px; padding: 0px; width: 660px; overflow: hidden; clear: both;">		
# 官方的最優(yōu)批量插入INSERT INTO yourtable VALUES (1,2), (5,5), ...;

部署操作。程序在抓取過程中,有可能會出現(xiàn)異常掛掉,為了保證高效穩(wěn)定,盡可能的寫一個定時腳本。每隔一段時間干掉,重新跑,這樣即使異常掛掉也不會浪費太多寶貴時間,畢竟,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 &  

數(shù)據(jù)分析呈現(xiàn)

數(shù)據(jù)的呈現(xiàn)主要使用echarts 3.0,感覺對于移動端兼容還不錯。兼容移動端的頁面響應(yīng)式布局主要通過幾個簡單的css控制,代碼如下

// 獲取用戶頭像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];

不足和待學(xué)習(xí)

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

  1. php 采用multicul進行多線程。
  2. 正則匹配進一步優(yōu)化
  3. 部署和抓取過程采用redis提升存儲
  4. 移動端布局的兼容性提升
  5. js的模塊化和sass書寫css。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 日韩一级片一区二区三区 | 久久久资源网 | 久久999久久 | 91午夜在线观看 | 一级空姐毛片 | 欧美黄色免费视频 | 91在线播放国产 | 欧美特级黄色 | 国产精品成人久久 | 国产成人午夜精品 | 深夜免费视频 | 久久伊 | 一级尻逼视频 | 成人偷拍片视频在线观看 | 亚洲一区免费观看 | 草久在线 | 一级黄色大片在线观看 | 国产精品久久国产精麻豆96堂 | 人禽l交免费视频观看 视频 | 久久久一区二区 | porno video hd 365hd | 999久久久国产999久久久 | 精品免费在线视频 | 无遮挡一级毛片视频 | 亚州精品天堂中文字幕 | 免费国产网站 | 成年人黄色免费电影 | 性视频久久 | 久久丝袜脚交足黄网站免费 | 欧美女孩videos | 久久影院在线观看 | 国产午夜亚洲精品理论片大丰影院 | 99激情 | 国产91九色 | 国产伊人色| 久久国产精品小视频 | 免费一级肉体全黄毛片 | 在线看国产视频 | 护士xxxx | 国产成人高清在线观看 | 久久久精品视频免费看 |