浅谈利用php爬取和分析知乎用户数据的方法
本文给大家介绍的是利用php的curl编写的爬取知乎用户数据的爬虫,并分析用户的各种属性。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
移动端分析数据截图
pc端分析数据截图
整个爬取,分析,展现过程大概分如下几步,小拽将分别介绍
curl爬取知乎网页数据
正则分析知乎网页数据
数据数据入库和程序部署
数据分析和呈现
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页面中发现了新的用户,可用于爬虫 <a class="zm-item-link-avatar avatar-link" href="/people/new-user" data-tip="p$t$new-user">
ok,这样子就可以通过自己-》关注人-》关注人的关注人-》。。。进行不断爬取。接下来就是通过正则匹配提取该信息
代码如下:
// 匹配到抓取页面的所有用户 preg_match_all('/\/people\/([\w-]+)\"/i', $str, $match_arr); // 去重合并入新的用户数组,用户进一步抓取 self::$newUserArr = array_unique(array_merge($match_arr[1], self::$newUserArr));
到此,整个爬虫过程就可以顺利进行了。
如果需要大量的抓取数据,可以研究下curl_multi
和pcntl
进行多线程的快速抓取,此处不做赘述。
分析用户数据,提供分析
通过正则可以进一步匹配出更多的该用户数据,直接上码。
// 获取用户头像 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 ;结束 中文 preg_match('/<input.+name=\"?gender\"?.+value=\"?1\"?.+([\x{4e00}-\x{9fa5}]+).+\;/u', $str, $match_sex); $user_sex = $match_sex[1]; // 匹配地区 //<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]; // 匹配学历 // <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]; // 匹配关注话题数量 // 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]; // 关注人数 // <span class="zg-gray-normal">关注了 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];
在抓取的过程中,有条件的话,一定要通过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 -9 sleep 5s # 重新跑 nohup /home/cuixiaohuan/lamp/php5/bin/php /home/cuixiaohuan/php/zhihu_spider/spider_new.php &
数据分析呈现
数据的呈现主要使用echarts 3.0,感觉对于移动端兼容还不错。兼容移动端的页面响应式布局主要通过几个简单的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 ;结束 中文 preg_match('/<input.+name=\"?gender\"?.+value=\"?1\"?.+([\x{4e00}-\x{9fa5}]+).+\;/u', $str, $match_sex); $user_sex = $match_sex[1]; // 匹配地区 //<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]; // 匹配学历 // <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]; // 匹配关注话题数量 // 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]; // 关注人数 // <span class="zg-gray-normal">关注了 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视频教程》

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

隨著資料時代的到來,資料量以及資料類型的多樣化,越來越多的企業和個人需要取得並處理大量資料。這時,爬蟲技術就成為了一個非常有效的方法。本文將介紹如何使用PHP爬蟲來爬取大數據。一、爬蟲介紹爬蟲是一種自動取得網路資訊的技術。其原理是透過編寫程式在網路上自動取得並解析網站內容,並將所需的資料抓取出來進行處理或儲存。在爬蟲程序的演化過程中,已經出現了許多成熟

隨著網路的發展,網頁中的資訊量越來越大,越來越深入,許多人需要從海量的資料中快速地提取出自己需要的資訊。此時,爬蟲就成了重要的工具之一。本文將介紹如何使用PHP編寫高效能的爬蟲,以便快速且準確地從網路中獲取所需的資訊。一、了解爬蟲基本原理爬蟲的基本功能就是模擬瀏覽器去造訪網頁,並且取得其中的特定資訊。它可以模擬使用者在網頁瀏覽器中的一系列操作,例如向伺服器發送請

PHP爬蟲入門:如何選擇合適的類別函式庫?隨著網路的快速發展,大量的數據散落在各個網站中。為了取得這些數據,我們常常需要使用爬蟲來從網頁中擷取資訊。而PHP作為常用的網頁開發語言,也有許多適用於爬蟲的類庫可供選擇。然而,在選擇適合自己專案需求的類別庫時,我們需要考慮一些關鍵因素。功能豐富性:不同的爬蟲類庫提供了不同的功能。有些類別庫只能用於簡單的網頁抓取,而有些

網路爬蟲是一種自動化抓取網路資訊的程序,它可以在很短的時間內獲得大量的資料。然而,由於網路爬蟲具有可擴展性和高效性等特點,使得許多網站擔心可能會遭受爬蟲攻擊,因此採取了各種反爬策略。其中,PHP網路爬蟲常見的反爬策略主要包括以下幾種:IP限制IP限制是最常見的反爬蟲技術,透過限制IP的訪問,可以有效防止惡意的爬蟲攻擊。為了因應這種反爬策略,PHP網路爬蟲可

PHP爬蟲類的並發與多執行緒處理技巧引言:隨著網路的快速發展,大量的資料資訊儲存在各種網站上,取得這些資料已經成為許多業務場景下的需求。而爬蟲作為一種自動化取得網路資訊的工具,被廣泛應用於資料收集、搜尋引擎、輿情分析等領域。本文將介紹一種基於PHP的爬蟲類的並發與多執行緒處理技巧,並透過程式碼範例來說明其實作方式。一、爬蟲類的基本結構在實作爬蟲類的並發與多執行緒處

隨著網路的快速發展與普及,越來越多的數據需要被收集和處理。爬蟲,作為常用的網路爬取工具,可以幫助快速存取、擷取和整理網路資料。針對不同的需求,也會有多種語言實作爬蟲,其中PHP也是比較流行的一種。今天,我們就來講一講基於PHP的爬蟲實作方法及注意事項。一、PHP爬蟲實作方法初學者建議使用現成的函式庫針對初學者而言,可能需要累積一定的程式碼經驗和網絡

如何使用PHP爬蟲類自動填入表單並提交資料?隨著互聯網的發展,我們越來越需要從網頁上獲取數據,或自動填充表單並提交數據。而PHP作為一種強大的伺服器端語言,提供了眾多的工具和類別庫來實現這些功能。在本文中,我們將介紹如何使用PHP中的爬蟲類來自動填入表單並提交資料。首先,我們需要使用PHP中的curl函式庫來進行網頁資料的取得與提交。 curl庫是一個功能強大

隨著網路的不斷發展,大量的資料被儲存在各種網站上,這些資料對於商業和科學研究有著重要的價值。然而,這些數據不一定容易取得。此時,爬蟲就成為一種非常重要且有效的工具,它可以自動地存取網站並抓取資料。 PHP是一種流行的解釋性程式語言,它有著簡單易學、程式碼高效等特點,適合用來實現爬蟲。本文將從以下幾個面向來介紹如何使用PHP實作爬蟲以及抓取資料。一、爬蟲的工作原理爬
