gpt4 book ai didi

php - PHP中如何只获取p标签的alt属性?

转载 作者:可可西里 更新时间:2023-10-31 23:33:58 26 4
gpt4 key购买 nike

我正在编写一个只显示价格的脚本。如果我这样做:

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

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

cafeïnevrij 的 roodmerk500克

2 帕肯

价格每公斤 1.99

199

从网站上,您可以看到它是 199,这就是价格,但首先我只需要 199 在 <p></p> 中而且我要 。或 ,介于 199 之间,因此它将显示 1,99 或 1.99。

如果我这样做:

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

它只会回显 <sup></sup> 中的 99 个如果我这样做:

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

好吧...这没什么用我怎样才能只得到 1 和 99 并放置一个 .或者,在它之间所以它看起来像 1,99 或 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函数和数据库连接。

// 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)。然后你可以做类似的事情:

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

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

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 - PHP中如何只获取p标签的alt属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8256189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com