2015-06-17 51 views
1

我試圖在XPath上抓取網站上的最新消息。CURL XPATH只抓取第一格

的消息是在具有相同的名稱很多的div(並與一個叫「p_maj」級) 這裏的div的例子:

<div class="p_maj"> 
 
    <h1>10 juin 2015</h1> 
 
    <div class="z_b_important"> 
 
    <h2>Actualités du projet</h2> 
 
    <p>some text</p> 
 
    <p>some text</p> 
 
    <h2>Version Cristal </h2> 
 
    <p>some text</p> 
 
    <h2>Barèmes</h2> 
 
    <p>some text</p> 
 
    <ul> 
 
    <h2>Information</h2> 
 
    <p>some text</p> 
 
</div> 
 
    
 
<div class="p_maj"> 
 
    <h1>03 juin 2015</h1> 
 
    <h2>Barèmes</h2> 
 
    <p>some text</p> 
 
    <ul> 
 
    <h2>Outils</h2> 
 
    <p>some text</p> 
 
</div>

我想我的網頁上只有第一個Div(最新發布)。

這裏是我的捲曲腳本來獲取申報單(效果很好),但我不能找到一種方法只具備抗凍一個:

 <?php 
$curl = curl_init('http://mywebsite/maj.htm'); 
curl_setopt($curl, CURLOPT_FAILONERROR, true); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
$result = curl_exec($curl); 

$dom = new DOMDocument(); 
$res=$dom->loadHTML($result); 
$xpath = new DomXPath($dom); 
$class = 'p_maj'; 
$divs = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]"); 

foreach($divs as $div) { 
echo $dom->saveXML($div); 
} 
?> 

任何人都知道我可以使用什麼樣的instruuction的?

我很抱歉,但PHP的開發是不是我的專長......

感謝大家會回答。

回答

0

第一分度類= 「p_maj」

//div[@class="p_maj"][1] 
+0

你的意思是我應該relapce行 $的div = $ xpath->查詢(「// * [包含(CONCAT(」」, normalize-space(@class),''),'$ class')]「); 與 $ divs = $ xpath-> query(「// div [@ class =」p_maj「] [1]」); 對不起,但這似乎並不奏效。 – gsanso

+0

怎麼不行? – splash58

+0

Oups ...對不起:我終於明白你告訴我了。我將發佈最終的代碼。 非常感謝! – gsanso