从没有RSS feed的外部站点中拉出最新的新闻项目-PREG_MATCH()



我试图从此网站中获取最新的4个新闻:http://www.wolverinegreen.com/sports/m-wrestl/spec-rel/utva-m-wrestl-spec-rel.html

他们没有RSS feed,因此我一直在阅读使用PHP Preg_Match函数,但是语法有点令人困惑,我不确定如何确切地做。任何建议都将不胜感激,或者如果有一种更有效的方法我没有想到,那么我就对想法开放。

// Get the page's HTML
$html = file_get_contents("http://www.wolverinegreen.com/sports/m-wrestl/spec-rel/utva-m-wrestl-spec-rel.html");
// Create a DOMDocument object and load the html into it
$dom = new DOMDocument();
$dom->loadHTML($html);
// Create an XPath object using the DOMDocument
$xpath = new DOMXPath($dom);
// Query for the a link using xpath
$items = $xpath->query("//td[1]/div/div[1]/a");
// If we find something using that query
if($items->length)
{
    // Output each item
    foreach($items as $item)
        echo $item->nodeValue . " - " . $item->getAttribute("href") . "<br />";
}

最新更新