Search code examples
phphtml-parsing

parse image src with simple html dom


im trying to get src image from this link: https://www.scribd.com/book/348571030/The-Alice-Network-A-Novel

this what ive don but no luck

<?php

 include('simple_html_dom.php');

 $html = file_get_html('https://www.scribd.com/book/348571030/The-Alice-Network-A-Novel');

 $list = $html->find('div[class="class="auto__base_component auto__shared_react_document_image react_document_image""]',0);

 $list_array = $list->find('img');

 $list_array2['thumb']  = $list_array->find('img.loaded', 0)->src;

 for ( $i = 0; $i < sizeof($list_array2); $i++ ){
  echo $list_array2[$i]->plaintext;
  echo "<br>";
 }

?>

Solution

  • Actually I get it done using the php class DOMDocument by getting all images in that page and then you can select what you want:

    $html = file_get_contents('https://www.scribd.com/book/348571030/The-Alice-Network-A-Novel');
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $images = $doc->getElementsByTagName('img');
    foreach ($images as $image) {
        echo $image->getAttribute('src') . "\n";
    }
    

    I gues if you are looking for the cover img, it's the third image so get as below:

    echo $images[2]->getAttribute('src') . "\n";