繁体   English   中英

DOMXPath不起作用

[英]DOMXPath not working

我正在尝试在网上商店获取产品价格,但DOMXPath似乎没有用。

服务器正在运行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);

loadHTML用于从字符串加载HTML,从文件或url使用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);
} 

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

$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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM