Reputation: 565
Does anyone know why this wouldn't work?
foreach($html->find('tbody.result') as $article) {
// get retail
$item['Retail'] = trim($article->find('span.price', 0)->plaintext);
// get soldby
$item['SoldBy'] = trim($article->find('img', 0)->getAttribute('alt'));
$articles[] = $item;
}
print_r($articles);
Upvotes: 1
Views: 611
Reputation: 54992
It seems to me the original code should work. But simple_html_dom often breaks or behaves unpredictably.
I recommend using php's built-in DomXPath:
$dom = new DOMDocument;
@$dom->loadHTMLFile('http://www.amazon.com/gp/offer-listing/B002UYSHMM');
$xpath = new DOMXPath($dom);
$articles = array();
foreach($xpath->query('//tbody[@class="result"]') as $tbody){
$item = array();
$item['Retail'] = $xpath->query('.//span[@class="price"]', $tbody)->item(0)->nodeValue;
$item['SoldBy'] = $xpath->query('.//img/@alt', $tbody)->item(0)->nodeValue;
$articles[] = $item;
}
print_r($articles);
Upvotes: 0
Reputation: 11711
Try this:
$html = file_get_html('http://www.amazon.com/gp/offer-listing/B002UYSHMM');
$articles = array();
foreach($html->find('table tbody.result tr') as $article) {
if($article->find('span.price', 0)) {
// get retail
$item['Retail'] = $article->find('span.price', 0)->plaintext;
// get soldby
if($article->find('img', 0)) $item['SoldBy'] = $article->find('img', 0)->getAttribute('alt');
$articles[] = $item;
}
}
print_r($articles);
Upvotes: 1