如果代码xpath存在,则提取标题xpath

时间:2016-04-01 14:35:24

标签: php xpath

我正在尝试提取优惠券代码,如果代码存在,那么也会获得相应的标题,但无法这样做。

在下面的代码中,我能够正确提取优惠券代码,但如何获得相应的标题提取oo。正如你在链接中看到的,有些标题没有优惠券代码......

<?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/>";

        }
    }
}


?>

thanx fusion3k代码工作正常,但使用你的代码我尝试了不同的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>';
?>

1 个答案:

答案 0 :(得分:0)

如果你想要检索没有优惠券的盒子,你必须以不同的方式进行:检索所有盒子,并为每个盒子找出是否存在优惠券代码。

初始化数组以存储结果:

LessonTwoB

使用“coupon-list-item”类搜索框($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

最后,您可以使用神话般的淡化compact函数将子数组添加到 $found = $my_xpath->query( 'div[@class="coupon-actions"]/div/a/small', $node ); $coupon = ( $found->length ) ? $found->item(0)->nodeValue : '' ;

$result

如果需要,您还可以以类似方式添加相关优惠券:

    $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

phpFiddle demo