1
我想從這個URL http://www.homegate.ch/kaufen/105652197?3
檢索所有圖像。我在PHP中使用Xpaths。出於某種原因,我可以用Xpath檢索身體,但不能檢索圖像。這裏是我的腳本:PHP Xpath返回節點的身體,而不是圖像
<?php
$url = "http://www.homegate.ch/kaufen/105652197?3";
$body = '//body';
$img = '//img';
$html = file_get_contents($url);
# Call htmlentities as the $url content is not well-formatted: http://stackoverflow.com/questions/1685277/warning-domdocumentloadhtml-htmlparseentityref-expecting-in-entity
$html = htmlentities($html);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DomXPath($dom);
$query = $xpath->query($body);
if($query->length == 1)
echo $query->item(0)->nodeValue;
if($query->length < 1)
echo "Xpath for body is no good!";
$query = $xpath->query($img);
if($query->length == 1)
echo $query->item(0)->nodeValue;
if($query->length < 1)
echo "Xpath for image is no good!";
運行此腳本返回:
1. <!DOCTYPE html>..
2. Xpath for image is no good!
這是怎麼回事錯在這裏? - 爲什麼是XPath只工作body
而不是img
var_dump($ query)給你什麼? –
'object(DOMNodeList)#3(1){[「length」] => int(0)}' – user1965074