Picking value created at runtime

5

I'm trying to fetch a value contained in a tag ( span ) located in another domain using PHP and JavaScript. There are cases where I can get the required value but most of the value returned is null . I believe that when I give file_get_contents to search for span, and it does not appear, it is because it needs the scripts referring to the mount of the value. Has anyone had a similar situation and solved the problem?

Here is the code I'm using:

        $regex = '/\<span class="special-price-value"(.*?)?\>(.|\n)*?\<\/span\>/i';
        $url = file_get_contents("http://link.com.br");                                                         
        preg_match_all($regex, $url, $scripts);
        print_r($scripts);
    
asked by anonymous 03.02.2015 / 11:41

3 answers

1

I could not get the value with DOMdocument because it does not load values sent to the tags via JavaScript. My solution was to use the JavaScript itself to fetch the required values.

    
06.02.2015 / 13:20
3

Why complicate using Regex to parse HTML? Use DOM and Xpath :

<?php

$doc = new DOMDocument();
$doc->loadHTML(file_get_contents("http://link.com.br"));
$xpath = new DOMXpath($doc);

$spans = $xml->xpath('//span[@class="special-price-value"]');

foreach ($spans as $span)
{
    echo $span->nodeValue;;
}
    
03.02.2015 / 12:23
1

Henry instead of using regular expressions you could use the features of this library Simple HTML DOM , with it's very simple to scan the html see an example of the manual itself:

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');//seu site aqui

// Find all spans 
foreach($html->find('span') as $element) 
   echo $element . '<br>';
//$html->find('span[id=especial]')
    
03.02.2015 / 12:23