如何仅在PHP中获得a p标签的alt属性? [英] How can I only get the alt attribute of the a p tag in PHP?

查看:187
本文介绍了如何仅在PHP中获得a p标签的alt属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个仅以价格作为回应的脚本. 如果我这样做:

I'm working on a script that echo's only the price. If I do:

$alttag = $oNode['p'];
echo $alttag;

它将回显<p></p>中的所有内容. 因此它将回显:

It will echo everything in <p></p>. So it will echo:

cafeïnevrij的roodmerk 白500克

roodmerk of cafeïnevrij pak 500 gram

2 pakken

每公斤prijs 1,99

prijs per kilo 1,99

199

,您可以看到它的回声199,这是价格,但首先我只需要<p></p>中的199,然后我要.或,介于199之间,因此它将显示1,99或1.99.

from the website, so you can see it echo´s 199, that´s the price but first I ONLY need 199 in the <p></p> and I want . or , between 199 so it will show 1,99 or 1.99.

如果我这样做:

$alttag = $oNode['p sup'];
echo $alttag;

它将仅回显<sup></sup>中的99 如果我这样做:

It will only echo 99 out of <sup></sup> If I do:

$alttag = $oNode['p sup'];
$maintag = $oNode['p']->attr('alt');
echo $maintag . $alttag;

好吧...这无能为力 我怎样才能只得到1和99并放置一个.或之间,看起来是1,99还是1.99?

Well... This does nothing How can I only get the 1 and 99 and place a . or , between it so it will look like 1,99 or 1.99?

 <div class="item-prijs">
        <p>
            <cufon class="cufon cufon-canvas" alt="1" style="width: 27px; height: 42px; ">
                <canvas width="47" height="43" style="width: 47px; height: 43px; top: -1px; left: -2px; "></canvas>
                <cufontext>1</cufontext>
            </cufon>
            <sup>
                <cufon class="cufon cufon-canvas" alt="99" style="width: 24px; height: 20px; ">
                    <canvas width="35" height="21" style="width: 35px; height: 21px; top: -1px; left: -1px; ">
                    </canvas><cufontext>99</cufontext>
                </cufon>
            </sup>
        </p>
    </div>

完整代码:不包含php函数和datbase连接.

Complete code: without the includes php functions and datbase connection.

// Extracts offers from html and return in array
function extractSparOffers($url)
{
    loadPqUrl($url);
    //Test $dates  = extractDateRange(pq('.contentdatagrid td:first'));
    $oNodes = pq('.item');
    if($oNodes->count() == 0) throw new Exception('No offers were found.');

    foreach($oNodes as $oNode) {

        $oNode = pq($oNode);
        //Test $titleDescCell = $oNode['input#a']->parent();
        //Test $titleDescCell['img, input']->remove();
        $priceCell = $oNode['span.price1']->parent()->parent();

        // Get title and description
        $data['title']                          = $oNode['.item-content h3'];
        $data['description']                    = $oNode['.item-content p'];
        // Get prices (page may contain price ranges)


        $alttag = $oNode['p sup'];
        $maintag = $oNode['p']->attr('alt');
        echo $maintag;

        //echo $alttag;

        //$alttags=preg_match_all('/<img[^>]*alt="([^"]*)"/i', $html, $matches);
        $none = "0.00";
        $data['priceBefore']                    = $none;
        $data['priceAfter']                     = $alttag;
        //                                        $oNode['item-prijs p.sup.cufon cufon-canvas']->attr('alt') ;
        // Get image  
        $imgNode = $oNode['img:only-child'];
        if(count($imgNode) > 0)
            $img = getimg('http://www.spar.nl/' . $oNode['img:only-child']->
                          attr('src'));
        else $img = '';                                     
        $data['image']                          = $img;

    //Test  $data['dateStart']                      = $dates['start'];
    //Test  $data['dateEnd']                        = $dates['end'];
    $date                    =date('Y-m-d');
    $data['dateStart']  = date('Y-m-d',  strtotime("yesterday"));
    $data['dateEnd']                    = date('Y-m-d', strtotime("tomorrow"));
        $data = formatOfferStrings($data);

        $odTotal[] = $data;
    }

    return $odTotal;
}

spiderInit();
$offerData = extractSparOffers('http://www.spar.nl/aanbiedingen/');
//Test processNewOffers('Spar', $offerData, $offerData[0]['dateStart']);
processNewOffers('Spar', $offerData, $dates['start']);


?>

推荐答案

那么,这基本上就是价格的网络爬虫吗?我建议您考虑使用PHP的DOMDocument库来解析XML(实际上是XHTML).然后,您可以执行以下操作:

So is this basically a web crawler for prices? I would suggest you look into using PHP's DOMDocument library to parse XML (Which XHTML practically is). You could then do something like:

//create a new DOMDocument object
$xmlDoc = new DOMDocument();  
//load your html for parsing
$xmlDoc->loadHTML("<html><body>Your HTML Code<br></body></html>");
//select the element that you want the attribute from...you may need to use $xmlDoc->getElementsByTagName('p');
$p_element = $xmlDoc->getElementById('yourtag');
//get the attribute alt of the selected element
$alt = $p_element->getAttribute('alt');
//show alt attribute value
echo $alt;

这只是伪代码,不会解决您的问题,但是,它似乎比您尝试使用的解析器更好.查看这些链接以获取更多信息(我希望这会有所帮助):

This is just pseudo code and will not solve your problem, however it seems to be a better solution than the parser you are trying to use. Look at these links for more information (I hope this helps):

http://www.php.net/manual/en/domdocument. Construct.php

http://php.net/manual/en/domelement.getattribute.php

http://www.php.net/manual/en/domdocument. getelementsbytagname.php

http://www.php.net/manual/en/domdocument. getelementbyid.php

这篇关于如何仅在PHP中获得a p标签的alt属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆