【发布时间】:2014-10-16 06:06:19
【问题描述】:
我有一个可以提取本网站产品类别的 php 代码:http://www.tradeindia.com/。到目前为止,我已经设法只提取了类别。我如何制作它以便它也可以提取它旁边的产品编号,因为它不在任何类名中?
我的代码:
<?php
//header('Content-Type: text/html; charset=utf-8');
$grep = new DoMDocument();
@$grep->loadHTMLFile("http://www.tradeindia.com/");
$finder = new DomXPath($grep);
$class = "cate_menu";
$nodes = $finder->query("//*[contains(@class, '$class')]");
$total_L = 0;
foreach ($nodes as $node) {
$span = $node->childNodes;
echo '<br>' . $span->item(0)->nodeValue . ' : ';
}
?>
来自网站的源代码:
<td align="left" style="padding-left:8px;color:blue"><a href=/Seller/Agriculture/ class="cate_menu" >Agriculture</a>(100892)</td>
<td align="left" style="padding-left:8px;color:blue"><a href=/Seller/Apparel-Fashion/ class="cate_menu" >Apparel & Fashion</a>(237902)</td>
<td align="left" style="padding-left:8px;color:blue"><a href=/Seller/Automobile/ class="cate_menu" >Automobile</a>(78614)</td>
我需要括号内的数字。
【问题讨论】:
-
regex 在这种情况下会很好地完成这项工作。
-
如果可能的话我想用这种方式(domdocument)
-
你好,cael,你到底要刮哪些产品?特色产品?
-
顶部类别中的产品数量。我有类别列表(农业、服装和时尚),但我无法获得它旁边的数字(减去括号)。
标签: php html xpath web-scraping domdocument