Search code examples
phpgoutte

How to scrape background image inside div tag using Goutte


I am trying to scrape flipkart images using Goutte, but the problem is that the image URL is inside the style attribute of a <div>. I tried several ways to scrape the image, but I failed. Can someone help me?

Here is the code that I tried:

$output = $crawler->filter('img[class="_3togXc _3wp706"]')->each(function ($node) {
      print_r($node);

    echo '<img src="' . $node->attr('src') . '" alt="' . $node->attr('alt') . '">';
});

var_dump($output);

The image is inside this <div> tag:

<div class="_2_AcLJ _3_yGjX" style="background-image:url(https://rukminim1.flixcart.com/image/128/128/jwnusnk0/shoe/g/g/4/combo-3-1206-1077-1197-6-shoefly-multicolor-original-imaf9zgwfbgeb6vw.jpeg?q=70)"></div>

Solution

  • Use this filter if you want to scrape background image with style attribute.

    $images = $html->filter('[style^="background-image"]')->each(function (Crawler $node, $i) {
        return $node->outerHtml();
    });
    

    Thanks