我正试图在网上商店获得产品的价格,但DOMXPath doest似乎正在起作用。
服务器正在运行PHP5.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);发布于 2015-09-15 14:04:07
loadHTML用于从字符串加载HTML,从文件中加载或者使用loadHTMLFile来加载url。
$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);
} 发布于 2015-09-15 14:12:40
尝试添加/在url末尾添加/,否则文档为空,并按照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);
}https://stackoverflow.com/questions/32587690
复制相似问题