CÓDIGO

Tutorial PHP - Extrair dados do site

PHP Simple HTML DOM Parser

a library for extracting specific content from a website and publishing it on another website. Web scraping is the process of extracting data or content from a website. Extracted data or content can then be posted on other websites or saved to a database. Unlike screen scraping, which captures content as an image, web scraping extracts the underlying HTML code to retrieve the website's data or content.

Step 1:

Download the PHP Simple HTML DOM Parser library from the following address.

https://simplehtmldom.sourceforge.io/
https://simplehtmldom.sourceforge.io/manual.htm

Step 2:

In this example, the data will be values ​​of precious metals (Platinum, Palladium and Rhodium) taken from the website: https://www.kitco.com/market/

In your browser, press F11, or right-click and inspect.
Identify the fields from which you want to collect values. In this case, Platinum has the id PT-bid.

Step 3: Crie um ficheiro PHP para descarregar os dados e insira a biblioteca PHP Simple HTML DOM Parser.

<?php 
include_once('simple_html_dom.php');

Enter the path to the page you want to scrape data from.

$html = file_get_html('https://www.kitco.com/market/');

$date_start = date('Y-m-d H:i:s', time()); // grava a data 

Step 4: Identify elements, extract values, convert from ounces to grams. 1 ounce = 28.3495231 grams

$PT = $html->getElementById("PT-bid"); // identifica o elemento Platina.
$PT =$PT->plaintext; // Extrai o valor do campo.
$pt_g = ($PT/28.3495231); // Converte o valor de onças para gramas.
$PD = $html->getElementById("PD-bid"); // identifica o elemento Paládio.
$PD = $PD->plaintext; // Extrai o valor do campo.
$pd_g = ($PD/28.3495231); // Converte o valor de onças para gramas.
$RH = $html->getElementById("RH-bid"); // identifica o elemento Ródio
$RH = $RH->plaintext; // Extrai o valor do campo.
$rh_g = ($RH/28.3495231); // Converte o valor de onças para gramas.

Step 5: 

Convert values ​​from Dollar to Euro:
On the same page, identify the quote.

kitco market 3

 

foreach($html->find('div.item_container.exchange_rates table tr td.index') as $e){
$arr[] = trim($e->innertext);
}

$dollar = $arr[2];

$PT_Euro = (number_format($pt_g,"2")*$dollar);
$PD_Euro = (number_format($pd_g,"2")*$dollar);
$RH_Euro = (number_format($rh_g,"2")*$dollar);

Step 6: Present Data

<div class="container">
<?php echo $date_start ." <b>Cotação Euro/Dollar</b>: ". $dollar ."<hr>"; ?> <div class="row"> <div class="col-sm-4"> <div class="card"> <div class="card-body"> <h5 class="card-title">PLATINUM</h5> <p class="card-text"> Oz/Dollar: <?php echo number_format($PT,"2");?><br/> Gr/Dollar: <?php echo number_format($pt_g,"2");?><br/> Gr/Euro: <?php echo number_format($PT_Euro,"2");?><br/> </div> </div> </div> <div class="col-sm-4"> <div class="card"> <div class="card-body"> <h5 class="card-title">PALLADIUM</h5> <p class="card-text"> Oz/Dollar: <?php echo number_format($PD,"2");?><br/> Gr/Dollar: <?php echo number_format($pd_g,"2");?><br/> Gr/Euro: <?php echo number_format($PD_Euro,"2");?><br/> </div> </div> </div> <div class="col-sm-4"> <div class="card"> <div class="card-body"> <h5 class="card-title">RHODIUM</h5> <p class="card-text"> Oz/Dollar: <?php echo number_format($RH,"2");?><br/> Gr/Dollar: <?php echo number_format($rh_g,"2");?><br/> Gr/Euro: <?php echo number_format($RH_Euro,"2");?><br/> </p> </div> </div> </div> </div> </div>

Step 7: Caso deseje gravar os dados numa base de dados, pode fazê-lo da seguinte forma:

<?php 
$sql = "INSERT INTO NOME-DA-TABELA (platinum, palladium, rhodium, date_start) VALUES ($PT_Euro, $PD_Euro, $RH_Euro, '$date_start')";
if(mysqli_query($conn, $sql)){
    echo '<div class="container">Os valores foram Gravados na base de dados.</div>';
} else{
    echo '<div class="container">ERROR: Could not able to execute $sql. ' . mysqli_error($conn) . '</div>';
}
?>

 Final Result:

PHP Simple HTML DOM Parser