[英]DOMXPath not working
I am trying to get the price of products in a webshop but DOMXPath doest seem to be working. 我正在尝试在网上商店获取产品价格,但DOMXPath似乎没有用。
The server is running php 5.5 and LibXML is enabled. 服务器正在运行php 5.5并启用了LibXML。 No errors are returned, only a length of zero.
不返回错误,仅返回零长度。
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 is for loading HTML from a string, to load from file or url use loadHTMLFile . 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);
}
Try adding / at the end of the url otherwise the document is empty and use loadHTMLFile as Danijel suggested: 尝试在网址末尾添加/,否则文档为空,并按照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.