I have a code that will scrap the data from a website. The output is something like this:
Agriculture
Food
Apparel
How do I only output the first/nth category such as only (Agriculture)? I tried
echo $sub_title[1].'<br/>';
but doesn't seems to be working.
My code:
<?php
$ch = curl_init('http://www.alibaba.com/Products');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$html = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$finder = new DOMXPath($dom);
$nodes = $finder->query('//h4[@class="sub-title"]');
foreach ($nodes as $node) {
$sub_title = trim(explode("\n", trim($node->nodeValue))[0]);
echo $sub_title.'<br/>';
}
?>