0
これを取得する方法は複数ありましたが、ウェブページの情報がほとんど見つからない理由はわかりません。こすりするxpathを使用してウェブページから特定のテキストを掻き集める
ページ: https://m.safeguardproperties.com/
情報が必要: バージョンのAppleのためのPhotoDirectの番号(現在は4.4.0)
のXpathを必要に応じて(と思う)テキストに:/ 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>';
?>
Chromeでxpath式を検証するには、$ x()コマンドライン関数を使用します。その作業が完了したら、検証済みの式をPHPに入れます。 – Baldy