DOMXPath无法正常工作

时间:2015-09-15 13:42:47

标签: php domxpath

我试图在网上商店获得产品的价格,但DOMXPath doest似乎正在运作。

服务器正在运行php 5.5并启用了LibXML。不返回任何错误,只有零长度。

 ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
session_start();

$xmlsource = 'https://tennistoko.nl/product/professional-supreme-comfort-grip-3-st';

$d = new DOMDocument();
$d->loadHTML($xmlsource);
$xpath = new DOMXPath($d);




$nodes = $xpath->query('//*[@itemprop]');  //this catches all elements with itemprop attribute
foreach ($nodes as $node) { 
   // do your stuff here with $node
   print_r($node);
}    


print_r($nodes);

2 个答案:

答案 0 :(得分:1)

loadHTML用于从字符串加载HTML,从文件或网址加载使用loadHTMLFile

$xmlsource = 'https://tennistoko.nl/product/professional-supreme-comfort-grip-3-st';

$d = new DOMDocument();
@$d->loadHTMLFile($xmlsource);      // @ if for suppressing warnings
$xpath = new DOMXPath($d);

$nodes = $xpath->query('//*[@itemprop]');  //this catches all elements with itemprop attribute
foreach ($nodes as $node) { 
   // do your stuff here with $node
   print_r($node);
} 

答案 1 :(得分:0)

尝试在网址末尾添加/,否则文档为空,并使用loadHTMLFile,如Danijel建议的那样:

$xmlsource = 'https://tennistoko.nl/product/professional-supreme-comfort-grip-3-st/';//changed your code here
$d = new DOMDocument();
@$d->loadHTMLFile($xmlsource);      // @ if for suppressing warnings
$xpath = new DOMXPath($d);
$nodes = $xpath->query('//*[@itemprop]');  //this catches all elements with   itemprop attribute
foreach ($nodes as $node) { 
  // do your stuff here with $node
  print_r($node);
}