0
我已经搜索并尝试了多种方式来获取此信息,但我不确定它为什么无法找到网页上的大部分信息。使用xpath从网页中刮掉特定文本
页凑: https://m.safeguardproperties.com/
信息需要:以文本所需的苹果(目前为4.4.0)
的XPath PhotoDirect 版本号(我认为):/ HTML /体/ DIV [ 1]/DIV [2]/DIV [1]/DIV [4]/DIV [3] /一个
尝试:
<?php
$file = "https://m.safeguardproperties.com/";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("/html/body/div[1]/div[2]/div[1]/div[4]/div[3]/a");
echo "<PRE>";
if (!is_null($elements)) {
foreach ($elements as $element) {
var_dump ($element);
echo "<br/>[". $element->nodeName. "]";
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
echo "</PRE>";
?>
第二次尝试:
<?PHP
$file = "https://m.safeguardproperties.com/";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
echo '<pre>';
// trying to find all links in document to see if I can see the correct one
$links = [];
$arr = $doc->getElementsByTagName("a");
foreach($arr as $item) {
$href = $item->getAttribute("href");
$text = trim(preg_replace("/[\r\n]+/", " ", $item->nodeValue));
$links[] = [
'href' => $href,
'text' => $text
];
}
var_dump($links);
echo '</pre>';
?>
您可以使用$ x()命令行函数在Chrome中验证您的xpath表达式。一旦你有这个工作,把验证的表达式放到你的PHP中。 – Baldy