Pull recent news items from external site with no rss feed - preg_match()? [closed]

狂风中的少年 提交于 2019-12-25 03:07:09

问题


I am trying to pull the latest 4 news items from this site here: http://www.wolverinegreen.com/sports/m-wrestl/spec-rel/utva-m-wrestl-spec-rel.html

They have no rss feed, so I have been reading into using php preg_match function but the syntax is a little confusing and I am not sure exactly how to do it. Any suggestions would be truly appreciated or if there is a more efficient method that I have not thought of then I am open to ideas.


回答1:


// 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 />";
}


来源:https://stackoverflow.com/questions/21552254/pull-recent-news-items-from-external-site-with-no-rss-feed-preg-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!