1

I am trying to scrape a few images from a website by using the PHP Simplehtmldom library. Normally the image url is found in the 'src' but in this case the image url is found in the 'data-url'. I am having trouble to access the data-url value and I was hoping some could help me out.

Let's say I have the following code:

<section id="imagesContainer">
    <div class="img">
        <img data-src="https://example.com/image1.jpg" alt="imageAlt1">
    </div>
    <div class="img">
        <img data-src="https://example.com/image2.jpg" alt="imageAlt2">
    </div>
    <div class="img">
        <img data-src="https://example.com/image3.jpg" alt="imageAlt3">
    </div>
</section>

I attempted to extract the image urls from the data-src with the following code but it doesn't return the image url:

foreach($html->find('#imagesContainer') as $imagesContainer) {
    foreach($imagesContainer->find('img') as $image) {
        echo $image->data-src;

    }

}

How can I extract the image url from the data-src? Is it possible with the simplehtmldom or do I need a regex?

1

1 Answer 1

1

many thanks for your suggestion. This code, using XPath, works to extract the value of the data-url:

 $image->getAttribute('data-src')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.