獲取SegmentFault某個標籤下的一些信息

前言

目前SegmentFault尚未開放API,17年的時候高陽大大說有計劃,雖然那會我還沒用SegmentFaultphp

PHP隨便寫了一個獲取某個標籤下信息的代碼,沒什麼技術含量,用來定時獲取html

目的是若是有問題能夠解決就幫助解決一下,或者是廣告的話能夠進行舉報node

獲取URL

主要獲取標籤動態下的就能夠了,包含了技術問答和專欄文章git

  1. 標籤動態 https://segmentfault.com/t/*
  2. 技術問答 https://segmentfault.com/t/*/questions
  3. 專欄文章 https://segmentfault.com/t/*/blogs

XPath節點

PHP使用XPath採集也是還能夠的,也相對簡單github

  1. 標籤動態json

    • 標題 //h4/a/text()
    • 連接 //h4/a/@href
  2. 技術問答segmentfault

    仔細點的話能夠帶一個 class屬性
    • 標題 //h2[@class="title"]/a/text()
    • 連接 //h2[@class="title"]/a/@href
  3. 專欄文章api

    • 標題 //h2/a/text()
    • 連接 //h2/a/@href

效果截圖

效果截圖

使用

須要補全代碼中的相關信息,URLXPath節點以及json文件路徑bash

可選擇使用 @Easy 的 PushBear 進行一對多推送,須要補全keyapp

crontab定時

crontab定時任務30分鐘一次,獲取第一頁的信息

*/30 * * * * php /www/wwwroot/tag.php >> /tmp/sf.log

PHP代碼

<?php
$url = ''; // 須要採集的URL
$key = ''; // PushBear的SendKey
$title_xpath = ''; // 標題的XPath節點
$url_xpath = ''; // 對應連接的XPath節點
$json_path = "/tmp/sf.json";

$html = file_get_contents($url);

$dom = new DOMDocument();
// 從一個字符串加載HTML
@$dom->loadHTML($html);
// 使該HTML規範化
$dom->normalize();
// 用DOMXpath加載DOM,用於查詢
$xpath = new 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); // 連接
for ($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 {
        // 相同就不發了
        echo date('Y-m-d H:i:s', time()). "內容相同".PHP_EOL;
          return;
    }
} else {
    // 不存在 寫文件
    file_put_contents($json_path, $json);
}

$str = "";
foreach ($data as $key => $item) {
    $num = $key + 1;
    $str .= "{$num}. [{$item['title']}]({$item['url']}) \n\n";
}

// 推送
if (!empty($key)) {
    echo sendByBear('***標籤動態', $str);
}

function getDiffArrayByTitle($arr1, $arr2, $pk='title'){
    $res = [];
    foreach($arr2 as $item) $tmpArr[$item[$pk]] = $item;
    foreach($arr1 as $v) if(! isset($tmpArr[$v[$pk]])) $res[] = $v;
    return $res;
}

function sendByBear($text, $desp = '', $key = '')
{
    $postData = http_build_query(
        array(
            'text' => $text,
            'desp' => $desp,
            'sendkey' => $key
        )
    );

    $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, $context);

    return $result;
}

結語

放個Github倉庫連接,若是本文有侵權思否官方能夠刪除😷

相關文章
相關標籤/搜索