Search code examples
javascriptphpweb-scrapingsimple-html-dom

How to get javascript values too with the php simple html dom?


I am using the Simple HTML DOM http://simplehtmldom.sourceforge.net/

?php

include_once('simple_html_dom.php');

$content = file_get_html('https://www.mesemix.hu/hu/superman-ruhanemuk/11292-szuperhosoek-mintas-zokni.html')->plaintext;

echo $content;
?>

The problem is that, the webshop that I am trying to scrape has some javascript in it which contains important values, that I need, like this:

var productReference = 'SP- 418070';

This is the webshop's source.

Does anyone has any idea how to get the "SP- 418070" int the plaintext too?


Solution

  • The thing you do is:

    Go to their store and press F12 after that click on "Elements tab" you can see all the code in there the selector for the model that you are looking for is :

    .product_reference .editable
    

    if you need to find something just use the ctrl+f for the search menu.

    If your code is structured like the demo version on Simple HTML dom

    $html->find('.product_reference .editable', 0)->innertext;
    

    EDIT Use curl , run this code somewhere and you will get the whole web contents

    <?php
    header('content-type:text/plain');
    // define the URL to load
    $url = 'example.com'; //THE URL THAT YOU NEED
    // start cURL
    $ch = curl_init(); 
    // tell cURL what the URL is
    curl_setopt($ch, CURLOPT_URL, $url); 
    // tell cURL that you want the data back from that URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    // run cURL
    $output = curl_exec($ch); 
    // end the cURL call (this also cleans up memory so it is 
    // important)
    curl_close($ch);
    // display the output
    echo $output;
    ?>