zl程序教程

您现在的位置是:首页 >  其它

当前栏目

xpath

xpath
2023-09-14 09:12:16 时间
<?php
$url = 'http://www.nipic.com/show/15984788.html';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, fopen('php://stdout', 'w'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($ch); 
curl_close($ch);

// create document object model
$dom = new DOMDocument();
// load html into document object model
@$dom->loadHTML($html);
// create domxpath instance
$xPath = new DOMXPath($dom);
// get all elements with a particular id and then loop through and print the href attribute
$elements = $xPath->query('//*[@id="J_detailMain"]/div[1]/h1');
var_dump($elements);
foreach ($elements as $e) {
  echo ($e->nodeValue);
}