I want to parse some products link, name and price. Here's my code: Having some trouble parsing, because I don't know how to get product link's and name's.Price is ok, I get it. And pagination not working as well
<h2>Telefonai Pigu</h2>
</br>
<?php
include_once('simple_html_dom.php');
$url = "http://pigu.lt/foto_gsm_mp3/mobilieji_telefonai/";
// Start from the main page
$nextLink = $url;
// Loop on each next Link as long as it exsists
while ($nextLink) {
echo "<hr>nextLink: $nextLink<br>";
//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a url
$html->load_file($nextLink);
$phones = $html->find('div#productList span.product');
foreach($phones as $phone) {
// Get the link
$linkas = $phone->href;
// Get the name
$pavadinimas = $phone->find('a[alt]', 0)->plaintext;
// Get the name price and extract the useful part using regex
$kaina = $phone->find('strong[class=nw]', 0)->plaintext;
// This captures the integer part of decimal numbers: In "123,45" will capture "123"... Use @([\d,]+),?@ to capture the decimal part too
echo $pavadinimas, " #----# ", $kaina, " #----# ", $linkas, "<br>";
//$query = "insert into telefonai (pavadinimas,kaina,linkas) VALUES (?,?,?)";
// $this->db->query($query, array($pavadinimas,$kaina, $linkas));
}
// Extract the next link, if not found return NULL
$nextLink = ( ($temp = $html->find('div.pagination a[="rel"]', 0)) ? "https://www.pigu.lt".$temp->href : NULL );
// Clear DOM object
$html->clear();
unset($html);
}
?>
Output:
nextLink: http://pigu.lt/foto_gsm_mp3/mobilieji_telefonai/
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/pigu_view.php
Line Number: 26
#----# 999,00 Lt #----#
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/pigu_view.php
Line Number: 26
Please Inspect carefully the source code you're working on, then, based on that, you can retrive the nodes you want... It's normal that the compatible code with another website wont work here, since the two websites dont have the same source code/structure !
Lets proceed, again, step by step...
$phones = $html->find('div#productList span.product');
will give you all "phones containers", or what I called "blocks"... Each block has the following structure:The anchor containing the product link an is included within
<p class="productPhoto">
, and it is the only anchor in there, so, to retrieve it simply use$linkas = $phone->find('p.productPhoto a', 0)->href;
(then complete it since it's only the relative link)The product name is located into
<h3>
tag, again, we use simply$pavadinimas = $phone->find('h3 a', 0)->plaintext;
to retrieve itThe price is included within
<div class="price"><strong>
, and again we use$kaina = $phone->find('div[class=price] strong', 0)->plaintext
to retrieve itHoever, not all phones have their price displayed, therefore, we must check if the price has been retrieved correctly or not
And finally, the HTML code containing the next link is the following:
So, we are interested in
<a rel="next">
tag, wich can be retrieved using$html->find('div#ListFootPannel a[rel="next"]', 0)
So, if we make add these modifications to your original code, we'll get:
Working DEMO