当前位置: 首页 > 后端技术 > PHP

在SegmentFault标签下获取一些信息

时间:2023-03-30 06:07:45 PHP

前言目前SegmentFault还没有开放API。2017年,高阳说他有一个计划。当时虽然没有用过SegmentFault,但是在PHP中写了一段代码获取某个标签下的信息。无技术含量??,用于定期获取目的。如果有可以解决的问题请帮忙解决,或者是广告的话可以举报获取网址,主要是获取标签动态,包括技术问答和专栏文章标签动态https://segmentfault.com/t/*技术问答https://segmentfault.com/t/*/questions专栏https://segmentfault.com/t/*/blogsXPath节点PHP也可以使用XPath来收集,和标签动态title比较简单//h4/a/text()link//h4/a/@href技术问答细心一点的可以带个class属性title//h2[@class="title"]/a/text()链接//h2[@class="title"]/a/@hrefColumn文章标题//h2/a/text()链接//h2/a/@href效果截图使用需要完成代码中的相关信息,URL,XPath节点和json文件路径可以选择使用@Easy的PushBear进行一对多推送,需要每隔30分钟完成一次keycrontab定时crontab定时任务,获取信息在第一页*/30****php/www/wwwroot/tag.php>>/tmp/sf.logPHPcodeloadHTML($html);//规范化HTML$dom->normalize();//使用DOMXpath加载DOM以供查询$xpath=新的DOMXPath($dom);//获取对应的xpath数据$title_hrefs=$xpath->query($title_xpath);//标题$data=[];for($i=0;$i<$title_hrefs->length;$i++){$href=$title_hrefs->item($i);$title=$href->nodeValue;$data[$i]['title']=$title;}//获取对应的xpath数据$url_hrefs=$xpath->query($url_xpath);//链接($i=0;$i<$url_hrefs->length;$i++){$href=$url_hrefs->item($i);$url=$href->nodeValue;$data[$i]['url']='https://segmentfault.com'.$url;}$json=json_encode($data);//判断文件是否存在if(file_exists($json_path)){//存在$old=file_get_contents($json_path);//文件不同if($old!=$json){//替换写入新文件file_put_contents($json_path,$json);$oldInfo=json_decode($old,true);//获取差异$data=getDiffArrayByTitle($data,$oldInfo);}else{//相同则不发送echodate('Y-m-dH:i:s',time())."内容相同".PHP_EOL;返回;}}else{//没有写文件file_put_contentts($json_path,$json);}$str="";foreach($dataas$key=>$item){$num=$key+1;$str.="{$num}.[{$item['title']}]({$item['url']})\n\n";}//推荐if(!empty($key)){echosendByBear('***标签动态',$str);}functiongetDiffArrayByTitle($arr1,$arr2,$pk='title'){$res=[];foreach($arr2作为$item)$tmpArr[$item[$pk]]=$item;foreach($arr1as$v)if(!isset($tmpArr[$v[$pk]]))$res[]=$v;返回$res;}functionsendByBear($text,$desp='',$key=''){$postData=http_build_query(array('text'=>$text,'desp'=>$desp,'sendkey'=>$键));$opts=array('http'=>array('method'=>'POST','header'=>'Content-type:application/x-www-form-urlencoded','content'=>$postData));$context=stream_context_create($opts);$result=file_get_contents('https://pushbear.ftqq.com/sub',false,$con文本);return$result;}结语放个Github仓库链接,如果本文有侵权,官方能不能删除?