簡體   English   中英

PHP $ xpath->查詢表達式不起作用

[英]PHP $xpath->query expression not working

PHP xpath查詢無法正常工作。 任何想法?

問題1
HTML來源:

<tr>
    <td class="abc pqr xyz">Some contents i want to capture</td>
</tr>
<tr>
    <td class="abc pqr xyz">more content i want to capture too</td>
</tr>
<tr>
    <td class="abc pqr xyz">all row in this table i want to capture</td>
</tr>
<tr>
    <td class="abc pqr xyz">they are all pokemon, i want to capture</td>
</tr>

我試過的PHP:

$url = "http://www.example.com/";

$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$text = file_get_contents($url,false,$context);

$dom = new DOMDocument();
@$dom->loadHTML($text);
$xpath = new DOMXPath($dom);

$divs = $xpath->query('//div/@class="abc pqr xyz"/');
foreach($divs as $b){
    //echo $b->name.'<br />';
    print_r($b);
}

但是什么都沒有,對於此查詢的正確表達式有什么幫助嗎?


問題2
我想檢查我是否正在獲取內容,所以我嘗試了一下並獲得了所有href鏈接:

$divs = $xpath->query('//a/@href');
foreach($divs as $b){
    print_r($b); // this is line #19
}

我收到此錯誤:

DOMAttr Object
Warning: print_r(): Not yet implemented in C:\xampp\htdocs\testing\index.php on line 19

任何想法,為什么我收到此警告?


問題3

                    <td colspan="2" style="">
                        <h3><a href="http://www.example.com/?id=xx" title="View more">I am not sure about the title</a>

                                <small class="comeoneman andwomen">Not a shoe</span>

                        </h3>

                        <div class="blahblah">This is just blah blah blah</div>                     

                    </td>
                    <td colspan="2" style="">
                        <h3><a href="http://www.example.com/?id=xx" title="View more">I am not sure about the title</a>

                                <small class="comeoneman andwomen">No a shoe</span>

                        </h3>

                        <div class="blahblah">This is just blah blah blah</div>                     

                    </td>

任何想法我怎么能得到這個信息,並將其轉換成這樣的數組:

array (
  title => I am not sure about the title,
  link => http://www.example.com/?id=xx,
  small => not a shoe,
  blahblah => This is just blah blah blah
)

問題1

根據您的標記,您正在嘗試定位<td>標簽,但是在您的查詢中,它是//div ,這沒有任何意義。 目標<td>的:

$rows = $xpath->query('//tr/td[@class = "abc pqr xyz"]');
foreach($rows as $b){
    echo $b->nodeValue . '<br/>';
}

樣本輸出

問題二

這很可能與以下問題有關:

https://bugs.php.net/bug.php?id=61858&edit=1

問題三

您可以繼續使用xpath定位所需的值。 選擇所有這些<td> ,然后從那里將它們中的每一個用作上下文節點:

$data = array();
$td = $xpath->query('//td');
foreach($td as $b){
    $data[] = array(
        'title' => $xpath->evaluate('string(./h3/a)', $b),
        'link' => $xpath->evaluate('string(./h3/a/@href)', $b),
        'small' => trim($xpath->evaluate('string(./h3/small)', $b)),
        'blahblah' => trim($xpath->evaluate('string(./div[@class="blahblah"])', $b)),
    );
}

樣本輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM