scraping problem with dom

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
siderp21
Forum Newbie
Posts: 2
Joined: Sun Aug 14, 2011 4:52 am

scraping problem with dom

Post by siderp21 »

i am trying to get price witch is in div tag with class='cost', but i get much more results because of divs with classes ="cost cost_with_old". What should i do?

http://www.ishop.lt/mac-kompiuteriai/ma ... mc700.html

Code: Select all

function extract_numbers2($string)
{
preg_match_all('/([\d]+)/', $string, $match);

return $match[0];
}


function get_data($url)
{
 $ch = curl_init();
 $timeout = 5;
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5');
 $data = curl_exec($ch);
 curl_close($ch);
 return $data;
}


$url = 'http://www.ishop.lt/mac-kompiuteriai/macbook-pro/macbook-pro-mc700.html';
$htmlc = get_data($url);
$html = str_get_html($htmlc);


foreach($html->find('div[class="cost"]') as $e)
echo $e->outertext;
echo $e->innertext;
echo "<br><br>";

$numbers_array = extract_numbers2($e->innertext);

echo $numbers_array[0];
echo "<br><br>";
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: scraping problem with dom

Post by McInfo »

Code: Select all

foreach ($html->find('div[class=cost]') as $e) {
    if ($e->class == 'cost') {
        // Do something with $e
    }
}
Post Reply