Skip to content

Instantly share code, notes, and snippets.

@valdeir2000
Last active February 20, 2023 19:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valdeir2000/511c2ab8f414ae033dad25e92bf464eb to your computer and use it in GitHub Desktop.
Save valdeir2000/511c2ab8f414ae033dad25e92bf464eb to your computer and use it in GitHub Desktop.
StackOverflow #277329
<link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css" integrity="sha384-nn4HPE8lTHyVtfCBi5yW9d20FjT8BJwUXyWZT9InLYax14RDjBj46LmSztkmNP9w" crossorigin="anonymous">
<?php
$content = file_get_contents("https://www.infomoney.com.br/mercados/cambio");
$dom = new domDocument();
@$dom->loadHTML($content);
$xpath = new DOMXpath($dom);
$tables = $xpath->query("//table[@class=\"table-general\"]");
$values = $xpath->query(".//tbody/tr", $tables->item(0));
$currencies = [];
foreach($values as $value) {
$currency = $xpath->query(".//td", $value);
/* Acessa o conteúdo em texto do primeiro elemento TD */
$name = trim($currency->item(0)->textContent);
/**
* Acessa o conteúdo o segundo elemento da tag TD,
* após isso capturamos o "irmão" (próximo elemento na mesma raiz), no caso o IMG
* e então captura o atributo SRC
*/
$img = trim( $currency->item(1)->firstChild->nextSibling->getAttribute("src") );
/* Acessa o conteúdo em texto do terceiro elemento TD */
$purchasePrice = trim($currency->item(2)->textContent);
/* Acessa o conteúdo em texto do quarto elemento TD */
$salePrice = trim($currency->item(3)->textContent);
/* Armazenamos em um array para posteriormente exibir aos usuários. */
$currencies[] = [
"img" => $img,
"name" => $name,
"purchasePrice" => $purchasePrice,
"salePrice" => $salePrice,
];
}
?>
<table class="pure-table">
<thead>
<tr>
<th></th>
<th>Moeda</th>
<th>Compra</th>
<th>Venda</th>
</tr>
</thead>
<tbody>
<!-- Percorre todo o array criado -->
<?php foreach($currencies as $currency): ?>
<tr>
<!-- Concate o endereço do site com o caminho da imagem -->
<td><img src="<?php echo "http://www.infomoney.com.br{$currency['img']}" ?>" /></td>
<!-- Exibe nome da moeda -->
<td><?php echo $currency['name'] ?></td>
<!-- Exibe valor da compra -->
<td><?php echo $currency['purchasePrice'] ?></td>
<!-- Exibe valor da venda -->
<td><?php echo $currency['salePrice'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css" integrity="sha384-nn4HPE8lTHyVtfCBi5yW9d20FjT8BJwUXyWZT9InLYax14RDjBj46LmSztkmNP9w" crossorigin="anonymous">
<?php
require_once "vendor/autoload.php";
$qp = html5qp("https://www.infomoney.com.br/mercados/cambio");
$values = $qp->find("table.table-general:first tbody tr");
foreach($values as $value) {
/* Armazenamos em um array para posteriormente exibir aos usuários. */
$currencies[] = [
"img" => trim($value->find('td:eq(2) img')->attr("src")),
"name" => trim($value->find('td:eq(1)')->text()),
"purchasePrice" => trim($value->find('td:eq(3)')->text()),
"salePrice" => trim($value->find('td:eq(0)')->text()),
];
}
?>
<table class="pure-table">
<thead>
<tr>
<th></th>
<th>Moeda</th>
<th>Compra</th>
<th>Venda</th>
</tr>
</thead>
<tbody>
<!-- Percorre todo o array criado -->
<?php foreach($currencies as $currency): ?>
<tr>
<!-- Concate o endereço do site com o caminho da imagem -->
<td><img src="<?php echo "http://www.infomoney.com.br{$currency['img']}" ?>" /></td>
<!-- Exibe nome da moeda -->
<td><?php echo $currency['name'] ?></td>
<!-- Exibe valor da compra -->
<td><?php echo $currency['purchasePrice'] ?></td>
<!-- Exibe valor da venda -->
<td><?php echo $currency['salePrice'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment