Using the explode() function on Infomoney

Asked

Viewed 275 times

0

I’m trying to put some economic indicators on the site and got a script that brings the dollar quotation from the Infomoney site.

However, I’m looking for other indexes, but the result of the field is composed and separated by spaces. The code looks like this:

if(!$fp=fopen("https://www.infomoney.com.br/mercados/acoes-e-indices" , "r" )) 
{
    echo "Erro ao abrir a página de indices" ;
    exit;
}
$conteudo = '';
while(!feof($fp)) 
{ 
    $conteudo .= fgets($fp,1024);
}
fclose($fp);

$valorCompraHTML    = explode('class="numbers">', $conteudo); 
// Esta é a variável que eu preciso explodir
$campo5             = trim(strip_tags($valorCompraHTML[5]));

//Estes são os valores HTML para exibir no site.    
$ibovespa = trim(strip_tags($valorCompraHTML[5]));
$ibovespa = explode(' ', $ibovespa);
$bovespa  = trim($ibovespa[0]) ;

The unexploded field looks like this: 74,294 +0.99 and the result of this variable is coming only 74,294.

I needed to know how to return the second value, the +0.99

  • Well, instead of 0 puts 1.

  • I tried to do this but returns empty field...

  • If any answer is useful, please mark it as accepted, see here why https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

3 answers

2

Utilise explode to handle and find HTML, and PHP has native resources for this... For me it is a huge gambit.

You can use Xpath (or another resource) to locate that element directly. The number is inside a td with class positivo or negativo, in addition this table is within a div with id of pnlContentA.

So just do:

//div[contains(@id, "pnlContentA")]//td[contains(@class,"numbers") and (contains(@class, "positivo") or contains(@class, "negativo"))]

In the case of PHP:

$Numbers = $XPath->query('
//div[contains(@id, "pnlContentA")]
//td[contains(@class,"numbers") and (contains(@class, "positivo") or contains(@class, "negativo"))]
');

You can test the above Xpath on Chrome itself, on the console under "Elements" and then "Ctrl+F" and paste the above Xpath.


In the end I’d have something like this:

libxml_use_internal_errors(true);

$ch = curl_init('http://www.infomoney.com.br/mercados/acoes-e-indices');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FAILONERROR => 1,
    CURLOPT_PROTOCOLS => CURLPROTO_HTTP,
]);

$conteudo = curl_exec($ch); 

if($conteudo !== false) {
    $DOM = new DOMDocument();
    $DOM->loadHTML($conteudo);

    $XPath = new DOMXPath($DOM);

    $Numbers = $XPath->query('
    //div[contains(@id, "pnlContentA")]
    //td[contains(@class,"numbers") and (contains(@class, "positivo") or contains(@class, "negativo"))]
    ');

    foreach ($Numbers as $number){
        echo htmlentities($number->nodeValue, ENT_QUOTES, 'utf-8');
        echo '<br>';
    }

}

Curl will request and return the body of the page, or it will fail immediately. Then, we create the DOM and import the HTML using the $DOM->loadHTML($conteudo).

Now the main part, we located the div that contains a id of pnlContentA, then we locate a td within it that has the class of Numbers and also that it has the class of positivo or negativo.

This is as a result:

+0,99
+1,05
+1,00
+1,67
+1,20
+1,47
+1,34
+2,12
+0,80
+1,01
+1,43
+0,97
+1,93

This site is unsafe, does not have HTTPS, the connection is always redirected to HTTP. Because of this I did not make a point of using any Curl resource.

  • 2

    +1 ... PS: it’s not just a question of gambiarra, it’s a matter of maintenance or rather, why use explodes if there are functions for this purpose. : D

  • is true, but hence the question should be another "What’s the best way ...."

0

If the field is coming 74.294 +0,99 and you have created an Array with explode():

$ibovespa = explode(' ', $ibovespa);

The elements of this Array will be:

ibovespa[0] = "74.294"
ibovespa[1] = "+0,99"

Soon, to take the second value (second element of the Array), just use the code:

$bovespa2  = trim($ibovespa[1]);
  • That’s what I thought. I’m using it like this: $valueVendaHTML = explode('+', strip_tags($valueCompraHTML[5]); //These are the HTML values to display on the site. $Ibovespa = Trim(strip_tags($valueCompraHTML[5]); $Ibovespa = explode(' ', $Ibovespa); $Bovespa = Trim($Ibovespa[1]) ; But it is returning empty field

  • Here comes the correct value. Take a look at this Ideone: https://ideone.com/S56u4K

0


There are several ways to get the desired, but I will stick to answers that meet the difficulty postulated in the question.

The value is taken by the class="Numbers positive element"

PHP

if(!$fp=fopen("https://www.infomoney.com.br/mercados/acoes-e-indices" , "r" )) 
{
    echo "Erro ao abrir a página de indices" ;
    exit;
}
$conteudo = '';
while(!feof($fp)) 
{ 
    $conteudo .= fgets($fp,1024);
}
fclose($fp);

$valorCompraHTML    = explode('class="numbers">', $conteudo); 
$ibovespa = trim(strip_tags($valorCompraHTML[5]));
$ibovespa = explode(' ', $ibovespa);
$bovespa  = trim($ibovespa[0]);

$valorMais    = explode('class="numbers positivo">', $conteudo); 
$mais = trim(strip_tags($valorMais[1]));
$mais = explode(' ', $mais);
$maisResult  = trim($mais[0]);

HTML

R$  <?php echo $bovespa  ?>  <?php echo $maisResult ?> 

Another way to get the same result

As stated in the author’s question O campo sem explodir está assim: 74.294 +0,99 does not correspond to reality, in fact the field returns like this,

campo

so we should take out all the extra spaces before the explosion to look like this 74.294 +0,99

if(!$fp=fopen("https://www.infomoney.com.br/mercados/acoes-e-indices" , "r" )) 
{
    echo "Erro ao abrir a página de indices" ;
    exit;
}
$conteudo = '';
while(!feof($fp)) 
{ 
    $conteudo .= fgets($fp,1024);
}
fclose($fp);

$valorCompraHTML    = explode('class="numbers">', $conteudo); 
// Esta é a variável que eu preciso explodir
$campo5             = trim(strip_tags($valorCompraHTML[5]));

//Estes são os valores HTML para exibir no site.    
$ibovespa = trim(strip_tags($valorCompraHTML[5]));

//retira todos os espaços em branco e retorna 74.294 +0,99
$ibovespa = preg_replace(array("/\t/", "/\s{2,}/", "/\n/", "/\r/"), array("", " ", " ", " "), $ibovespa);

$ibovespa = explode(' ', $ibovespa);

$bovespa  = trim($ibovespa[0]);

$mais  = trim($ibovespa[1]);

script for Dolar, euro etc...



The explode() was created with the intention of separating a string into an array of several smaller strings. For this he uses certain characters, passed by parameters, to make the separation.

In the programming, the gambiarra is a palliative (and creative) way to solve a problem or fix a system inefficiently, inelegant or incomprehensible, but still works.

This is a code that prints on the screen the repetition of the phrase "Hello world!" 5 times using for this a loop to control

#include <stdio.h>
int main (void) {
    int i;
    for (i=0; i<5; i++) {
        printf("Hello world!");
    }
    return 0;
}

This is a code that prints on the screen the repetition of the phrase "Hello world" 5 times, but using gambiarra to get the final result:

#include <stdio.h>
int main (void) {
    printf("Hello world!");
    printf("Hello world!");
    printf("Hello world!");
    printf("Hello world!");
    printf("Hello world!");

    return 0;
}
  • Sensational, now it worked. I would never figure it out myself. Thank you brother!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.