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

StackOverflow https://stackoverflow.com/questions/21552254

  •  06-10-2022
  •  | 
  •  

質問

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.

役に立ちましたか?

解決

// 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 />";
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top