2016-04-01 9 views
1

私はクーポンコードを抽出しようとしていますが、そのコードが存在する場合は対応するタイトルも取得できますが、そうすることはできません。コードのxpathが存在する場合はxpathを抽出

以下のコードでは、クーポンコードを正しく抽出できますが、どのようにして対応するタイトルを抽出するのですか?あなたには、いくつかのタイトルがクーポンコードをお持ちでないリンクで見ることができるように...コードfusion3k

<?php 



$html = file_get_contents('http://www.grabon.in/abof-coupons/'); //get the html returned from the following url 

$mydoc = new DOMDocument(); 

libxml_use_internal_errors(TRUE); //disable libxml errors 

if(!empty($html)){ //if any html is actually returned 

    $mydoc->loadHTML($html); 
    libxml_clear_errors(); //remove errors for yucky html 

    $my_xpath = new DOMXPath($mydoc); 

    //get all the codes 

    $my_code = $my_xpath->query('//*[@class="coupon-click"]//a//small'); 
    if($my_code->length > 0){ 
     foreach($my_code as $row){  
      $my_row = $my_xpath->query('//*[@class="h3_click"]'); 
      echo $code->nodeValue . "<br/>"; 

     } 
    } 
} 


?> 

ありがとうは完璧に動作しますが、私は以下のように別のURLのために試してみましたウルのコードを使用して、エラー通知を取得する:しようとしていますあなたはまた、クーポンのないボックスを取得したい場合は非オブジェクト

<?php 

$html = file_get_contents('http://official.deals/ebay-coupons?coupon-id=1055981&h=ed68f1b2a5b28471ecf9584734d65742&utm_source=coupon_page&utm_medium=deal_reveal&utm_campaign=od_deal_click#ebay1055981'); //get the html returned from the following url 

$mydoc = new DOMDocument(); 

libxml_use_internal_errors(TRUE); //disable libxml errors 

if(empty($html)) die("EMPTY HTML"); 

    $mydoc->loadHTML($html); 
    libxml_clear_errors(); //remove errors for yucky html 

    $my_xpath = new DOMXPath($mydoc); 

    ////////////////////////////////////////////////////// 
    $result = array(); 

    $nodes = $my_xpath->query('//div[@data-rowtype="1"]'); 
    foreach($nodes as $node) 
    { 
     $title = $my_xpath->query('div[@class="cop-head"]/h4', $node)->item(0)->nodeValue; 
     $found = $my_xpath->query('div[@class="cop-head"]/div/input/value', $node); 
     $coupon = ($found->length) ? $found->item(0)->nodeValue : '' ; 
     $result[] = compact('title', 'coupon'); 
    } 


echo '<pre>'; 
print_r($result); 
echo '</pre>'; 
?> 

答えて

0

のプロパティを取得、あなたは別の方法で進行しなければならない:クーポンコードが存在するかどうかを見つける、各ボックスのために、すべてのボックスを取得して。結果を格納する配列のInit

:ボックスの

$result = array(); 

が検索(クラスで<li>ノードが「クーポンリスト項目」):

$nodes = $my_xpath->query('//li[@class="coupon-list-item "]'); 
#              ↑ pay attention! 

そしてforeachループを介して各ノードを分析します:

foreach($nodes as $node) 
{ 

マッチタイトル:

$title = $my_xpath->query('div/b[@class="h3_click"]', $node)->item(0)->nodeValue; 
    #        ┊       ┊ 
    # No starting slashes, pattern is node-relative    ┊ 
    #   Second optional xpath->query parameter define the search context 

が存在する場合は、その後、クーポンを検索:

$found = $my_xpath->query('div[@class="coupon-actions"]/div/a/small', $node); 
    $coupon = ($found->length) ? $found->item(0)->nodeValue : '' ; 

を終わりに、あなたは素晴らしいを使用して$resultにサブアレイを追加することができますがcompact機能を軽視:

$result[] = compact('title', 'coupon'); 
} 

同様に、関連するクーポンを追加することもできます。

終わり
$nodes = $my_xpath->query('//div[@class="related-coupons"]/*/div[@class="col-sm-8"]'); 
foreach($nodes as $node) 
{ 
    $title = $my_xpath->query('div/div[@class="coupon-title"]', $node)->item(0)->nodeValue; 
    $found = $my_xpath->query('div/div[@class="coupon-click"]/a/small', $node); 
    $coupon = ($found->length) ? $found->item(0)->nodeValue : '' ; 
    $result[] = compact('title', 'coupon'); 
} 

は、$resultは次のようになります。

Array 
(
    [0] => Array 
     (
      [title] => Upto 80% OFF + Extra Rs. 500 OFF On Rs 1495 - All Users 
      [coupon] => ABOFBMF500C 
     ) 
    (...) 
    [14] => Array 
     (
      [title] => Fresh Arrivals on Women & Men Collection 
      [coupon] => 
     ) 
    (...) 
) 

phpFiddle demo

+0

ではなく、その空に、私はスキップすることができ、アレイへのすべての書き込みがとにかくありますか? –

+0

明らかにそこにそれがある!あなたはどのように理解できますか?それはあまりにも難しくありません...ループの中を見てみましょう: '$ coupon =($ found-> length)? $ found-> item(0) - > nodeValue: ''; 'if($ found-> length){$ coupon = ...;}と同じです。 } else {...} '...あなたが道を見つけたら教えてください! – fusion3k

+0

それは大丈夫だよ...あまりにも多く –

関連する問題