2017-03-05 16 views
0

次のコードがあります。このコードから「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>&nbsp;</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); 
} 
+0

私は、任意のPHPコードが表示されませんか、あなたのペーストビンの参照のXPATH。 – trincot

+0

XPathが機能します。あなたがそれらを適用するコード、あなたが得る結果、そしてあなたが期待したものを示してください。 – trincot

+0

[この例](https://eval.in/748195)のように '$ div-> textContent'を使用してください。 – trincot

答えて

0

Elementは: //のdiv [は含まれています(@class、 'コンテンツ')]/P [1] /強い

、その後のTextContent

やテキストを取る: は// divの[含まれています(@class、 'コンテンツ')]/P [1] /強い/テキスト()

とあなたのXMLが整形式ではありません。なぜなら<のBr>

+0

彼らは何も返さないので、仕事のどれも – user7342807

関連する問題