次のコードがあります。このコードから「Washington square-USA」部分を抽出しようとしています。 div/p/strong
の中にありますが、divにはクラスがあります。xpathでdiv内の子要素を取得できません
関連するコードである、またはあなたがそうentire code in pastebin
<div class="content clearfix">
<p><strong>Washington square - USA<br>
</strong></p>
<p><strong>2 studios for rent – env. 54m2</strong></p>
<p><strong>near public transport</strong></p>
<p>Studios comprise</p>
<ul>
<li>A kitchen</li>
<li>A bedroom</li>
<li>Tolilet with bathtab</li>
</ul>
<p>Visitation date (not yet known)</p>
<p>To rent from 1st april</p>
<p>(Current owner : Ben)</p>
<p><strong>For more details visit: http://example.com<br>
</strong></p>
<p><strong> </strong></p>
</div>
を見ることができ、私はコンテンツ
//div[contains(@class, "content")]/p/strong
//div[contains(@class, "content") and contains(@class, 'clearfix')]/p/strong
//div[contains(@class, "content") and contains(@class, 'clearfix')]/p[1]/strong
//string(div[contains(@class, "content") and contains(@class, 'clearfix')]/p/strong)
//div[contains(@class, "content") and contains(@class, 'clearfix')]/p/strong/text()
しかし、リターンのいずれも所望の出力を得るために、次の方法を試してみました
EDITこのコードを使用してページを解析しています
$document = new \DOMDocument();
$document->loadHTMLFile($htmlUrl);
$xpath = new \DOMXPath($document);
foreach ($xpath->evaluate('//div[contains(@class, "content")]//p[1]') as $div) {
# Also tried with these
//div[contains(@class, "content")]/p/strong
//div[contains(@class, "content") and contains(@class, 'clearfix')]/p/strong
//div[contains(@class, "content") and contains(@class, 'clearfix')]/p[1]/strong
//string(div[contains(@class, "content") and contains(@class, 'clearfix')]/p/strong)
//div[contains(@class, "content") and contains(@class, 'clearfix')]/p/strong/text()
var_dump($div);
}
私は、任意のPHPコードが表示されませんか、あなたのペーストビンの参照のXPATH。 – trincot
XPathが機能します。あなたがそれらを適用するコード、あなたが得る結果、そしてあなたが期待したものを示してください。 – trincot
[この例](https://eval.in/748195)のように '$ div-> textContent'を使用してください。 – trincot