用php抓取分析知乎用户数据
背景:萧也利用PHP curl编写的爬虫对知乎5w用户的基本信息进行了实验性抓取。同时,对抓取的数据进行了简单的分析和展示。
Php蜘蛛代码和用户仪表盘显示代码,整理后上传github,更新个人博客和微信官方账号中的代码库,程序仅供娱乐、学习和交流;如有侵犯知乎相关权益的,请尽快联系我删除。
没有图片,就没有真相。
手机分析数据截图。
电脑分析数据截图。
整个抓取、分析、展示的过程大致分为以下几个步骤,小野会分别介绍。
Curl抓取知乎网页数据常规分析知乎网页数据仓库和程序部署数据分析和呈现curl抓取网页数据。
PHP curl扩展是一个PHP支持的库,允许您使用各种类型的协议与各种服务器连接和通信。它是一个非常方便的网页抓取工具,也支持多线程扩展。
这个程序捕获了知乎提供给用户访问的个人信息页面。在抓取过程中,https://www.zhihu.com/people/xxx,需要携带用户cookie来获取页面。直接编码。
获取页面cookie。
复制代码如下://登录知乎,打开个人中心和控制台,获取cookie document . cookie ' _ za=67254197-3WWB8D-43 F6-94f 0-fb0e2d 521 c31;_ ga=GA1 . 2 . 2142818188 . 19999999996q _ C1=78ee 1604225d 47d 08 cddd 8142 a 08288 b 23 | 1452172601000 | 1452172601000;_ xsrf=15f 0639 cbe6fb 607560 c 075269064393;cap _ id=' n2qwmtexngq 0yt 2 ngvddlmgiynmq 4 njdjotu0ytm5 mmq=| 1453444256 | 49 FDC 6b 43 DC 51 f 702 b7d 6575451 e 228 f 56 CDA f5d ';_ _ utmt=1;unlock _ ticket=' qujdtwmm0 lszdd2d yqufqvlrslzuvtnvb1 zandvoqxjlblvmwgj 0 wgwyahlddvdscxdz u1vrpt 0=| 145344421 | c47 a2 afde 1 ff 334d 416 bafb1c c 267 b 41014 C9 d5f ';_ _ utma=51854390 . 21428 DD 18188 . 1433767421 . 3。_ _ utmb=51854390 . 14 . 8。44480 . 44444444411_ _ utmc=51854390_ _ utmz=51854390 . 1452846679 . 1 . DD 1 . utmcsr=Google | utmccn=(organic)| utmcmd=organic | utmctr=(未提供);__utmv=51854390.100-1|2=registration_date=20150823=1^dd3=entry_date=20150823=1'
抓取个人中心页面。
通过curl,携带cookie并首先抓取我的中心页面。
/* * *按用户名抓取个人中心页面并存储* * @param $username str :用户名标志* @return boolean :成功或失败标志*/public function spider user($ username){ $ cookie=' xxxx ';$ URL _ info=' http://www . zhi Hu.com/people/'。$ username//这里崔小专代表用户ID,可以直接看url得到我的ID $ ch=curl _ init($ URL _ info);//初始化会话curl _ setopt ($ ch,curl opt _ header,0);curl_setopt($ch,CURLOPT_COOKIE,$ COOKIE);//设置请求cookie curl _ setopt ($ ch,curl opt _ 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/zx data _ ch/PHP/胡志_spider/file/')。$用户名。html ',$ result);返回真;}定期分析网页数据分析的新链接并进一步抓取。
若要存储已爬网的网页,如果要进一步爬网,该网页必须包含可用于进一步爬网用户的链接。通过对知乎页面的分析,发现个人中心页面有关注者和一些喜欢和关注者。如下
复制的代码如下://在抓取的html页面中发现了一个新用户,可用于爬虫A class=' zm-item-link-avatar avatar-link ' href='/people/new-user ' data-tip=' p $ t $ new-user '。
好吧,这样,你就可以自己去关注人——关注人——了。继续爬。下一步是通过常规匹配提取这些信息。
复制代码如下://匹配捕获页面的所有用户preg _ match _ all('/\/people \/([\ w-])\ '/I ',$ str,$ match _ arr);//为了合并到一个新的用户数组中,用户进一步抓取self :3360 $ new user arr=array _ unique(array _ merge($ match _ arr[1],self 33603360 $ new user arr));
到此,整个爬虫过程就可以顺利进行了。如果需要大量的抓取数据,可以研究下卷曲_多和pcntl进行多线程的快速抓取,此处不做赘述。
分析用户数据,提供分析
通过正则可以进一步匹配出更多的该用户数据,直接上码。
//获取用户头像preg_match('/img .src=\ '?([^\s] \ .(jpg|gif|bmp|bnp|png))\ '?/i ',$str,$ match _ img);$ img _ URL=$ match _ img[1];//匹配用户名://span class='name '崔小拽/spanpreg_match('/span .类=\ '?名称\ '?([{ 4 e00 }-{ 9 F5 }]).span/u ',$str,$ match _ name);$ user _ name=$ match _ name[1];//匹配用户简介//班级生物跨度中文preg_match('/span .类=\ '?生物?\([\x{4e00}-\x{9fa5}]).span/u ',$str,$ match _ title);$ user _ title=$ match _ title[1];//匹配性别//输入类型=' radio ' name=' gender ' value=' 1 ' checked=' checked ' class=' maly '/男//性别值1;结束中文preg_match('/input .名称=\ '?性别\ '?值=\ '?1\'?([{ 4 e00 }-{ 9 F5 }]).\;/u ',$str,$ match _ sex);$ user _ sex=$ match _ sex[1];//匹配地区//span class=' location item ' title='北京preg_match('/span .类=\ '?位置。\'?\'([\x{4e00}-\x{9fa5}] )\'/u ',$str,$ match _ city);$ user _ city=$ match _ city[1];//匹配工作//span class='就业项目标题='人见人骂的公司'人见人骂的公司/spanpreg_match('/span .类=\ '?就业。\'?\'([\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 '数据-token=' 19590046 '数据-topic id=' 13253 '程序猿/a/spanpreg_match('/span .类=\ '?位置。\'?\'([\x{4e00}-\x{9fa5}]).\'/u ',$str,$ match _ position);$ user _ position=$ match _ position[1];//匹配学历//span class='教育项目标题='研究僧'研究僧/spanpreg_match('/span .类=\ '?教育。\'?\'([\x{4e00}-\x{9fa5}] )\'/u ',$str,$ match _ education);$ user _ education=$ match _ education[1];//工作情况//span class=' education-extra item ' title='挨踢'挨踢/spanpreg_match('/span .类=\ '?教育-额外。\'?([\x{4e00}-\x{9fa5}] )/u ',$str,$ match _ education _ extra);$ user _ education _ extra=$ match _ education _ extra[1];//匹配关注话题数量//class=' ZG-link-lit blue ' strong 41个话题/strong/apreg_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 '个人主页被strong17/strong人浏览/spanpreg_match('/class=\ '?ZG灰色-正常\ '?(\d).span/i ',$str,$ match _ browse);$ user _ browse=$ match _ browse[1];在抓取的过程中,有条件的话,一定要通过redis入库,确实能提升抓取和入库效率。没有条件的话只能通过结构化查询语言优化。这里来几发心德。
数据库表设计索引一定要慎重。在蜘蛛;状似蜘蛛的物体;星形轮;十字叉;连接柄;十字头爬取的过程中,建议出了用户名,左右字段都不要索引,包括主键都不要,尽可能的提高入库效率,试想5000w的数据,每次添加一个,建立索引需要多少消耗。等抓取完毕,需要分析数据时,批量建立索引。
数据入库和更新操作,一定要批量mysql。官方给出的增删改的建议和速度:http://dev。MySQL。com/doc/ref man/5.7/en/insert-speed。超文本标记语言
# 官方的最优批量插入将值(1,2)、(5,5)插入您的表中,部署操作。程序在抓取过程中,有可能会出现异常挂掉,为了保证高效稳定,尽可能的写一个定时脚本。每隔一段时间干掉,重新跑,这样即使异常挂掉也不会浪费太多宝贵时间,毕竟时间就是金钱。
#!/bin/bash#干掉PS aux | grep spider | awk " { print $ 2 } " | xargs kill-9 sleep 5s #重新跑nohup/home/崔小焕/lamp/PHP 5/bin/PHP/home/崔小焕/PHP/胡志_spider/spider_new.php数据分析呈现
数据的呈现主要使用echarts 3.0,感觉对于移动端兼容还不错。兼容移动端的页面响应式布局主要通过几个简单的钢性铸铁控制,代码如下
//获取用户头像preg_match('/img .src=\ '?([^\s] \ .(jpg|gif|bmp|bnp|png))\ '?/i ',$str,$ match _ img);$ img _ URL=$ match _ img[1];//匹配用户名://span class='name '崔小拽/spanpreg_match('/span .类=\ '?名称\ '?([{ 4 e00 }-{ 9 F5 }]).span/u ',$str,$ match _ name);$ user _ name=$ match _ name[1];//匹配用户简介//班级生物跨度中文preg_match('/span .类=\ '?生物?\([\x{4e00}-\x{9fa5}]).span/u ',$str,$ match _ title);$ user _ title=$ match _ title[1];//匹配性别//输入类型=' radio ' name=' gender ' value=' 1 ' checked=' checked ' class=' maly '/男//性别值1;结束中文preg_match('/input .名称=\ '?性别\ '?值=\ '?1\'?([{ 4 e00 }-{ 9 F5 }]).\;/u ',$str,$ match _ sex);$ user _ sex=$ match _ sex[1];//匹配地区//span class=' location item ' title='北京preg_match('/span .类=\ '?位置。\'?\'([\x{4e00}-\x{9fa5}] )\'/u ',$str,$ match _ city);$ user _ city=$ match _ city[1];//匹配工作//span class='就业项目标题='人见人骂的公司'人见人骂的公司/spanpreg_match('/span .类=\ '?就业。\'?\'([\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 '数据-token=' 19590046 '数据-topic id=' 13253 '程序猿/a/spanpreg_match('/span .类=\ '?位置。\'?\'([\x{4e00}-\x{9fa5}]).\'/u ',$str,$ match _ position);$ user _ position=$ match _ position[1];//匹配学历//span class='教育项目标题='研究僧'研究僧/spanpreg_match('/span .类=\ '?教育。\'?\'([\x{4e00}-\x{9fa5}] )\'/u ',$str,$ match _ education);$ user _ education=$ match _ education[1];//工作情况//span class=' education-extra item ' title='挨踢'挨踢/spanpreg_match('/span .类=\ '?教育-额外。\'?([\x{4e00}-\x{9fa5}] )/u ',$str,$ match _ education _ extra);$ user _ education _ extra=$ match _ education _ extra[1];//匹配关注话题数量//class=' ZG-link-lit blue ' strong 41个话题/strong/apreg_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 '个人主页被strong17/strong人浏览/spanpreg_match('/class=\ '?ZG灰色-正常\ '?(\d).span/i ',$str,$ match _ browse);$ user _ browse=$ match _ browse[1];不足和待学习
整个过程中涉及php,shell,js,css,html,正则等语言和部署等基础知识,但还有诸多需要改进完善,小拽特此记录,后续补充例:
服务器端编程语言(专业超文本预处理器的缩写)采用多保险商实验所进行多线程。正则匹配进一步优化部署和抓取过程采用redis提升存储移动端布局的兼容性提升射流研究…的模块化和厚颜无耻书写css。
版权声明:用php抓取分析知乎用户数据是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。