簡體   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