I have a php code that could extract the product categories in this website: http://www.tradeindia.com/. So far I had managed to extract only the categories. How do I make it so that it will also extract the product numbers beside it since its not in any class name?
My code:
<?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 . ' : ';
}
?>
Source code from website:
<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>
I need the numbers between brackets.
I'm not an xpath guru, but what I would do is to target first that particular table using that needle categories, then from there get those rows based on that and start looping on found rows.
Rough example:
Sample Output
Since the number is between two brackets, this should be easy. You can use a function like this;
You will need to get each line of the table inserted separately though. You could loop through an array of strings containing each line;
foreach($htmllines as $htmlline)
or similar.Hope this helps.