Last active
March 27, 2017 18:33
-
-
Save jjcodes78/f897d6224aad6394341d52e965dc04e4 to your computer and use it in GitHub Desktop.
Exemplo de parser com https://github.com/viion/PHP-Fast-Simple-HTML-DOM-Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: Jorge | |
* Date: 27/03/2017 | |
* Time: 13:31 | |
*/ | |
require 'vendor/autoload.php'; | |
use FastSimpleHTMLDom\Document; | |
$html = Document::file_get_html('http://cnes2.datasus.gov.br/Cabecalho_Reduzido_Competencia.asp?VCod_Unidade=3550306926622'); | |
//$html = Document::file_get_html('http://cnes2.datasus.gov.br/Cabecalho_Reduzido_Competencia.asp?VCod_Unidade=4120607381840'); | |
echo "Estab: http://cnes2.datasus.gov.br/Cabecalho_Reduzido_Competencia.asp?VCod_Unidade=3550306926622 \n"; | |
$tables = $html->find('table'); // indexa todas as tabelas do DOM | |
// $tables[5] é a tabela onde contém os "cabeçários" : Nivel Atenção , Atividade e Gestão | |
// children(0) é o <tr> - primeiro nó filho da tabela | |
// a partir daí o <tr> vai conter seus nós filhos e como eu vi no HTML que cada td há 1 filho a mais <font> | |
// então em <tr> vão ter filhos de 0...5 mas eu quero apenas o plaintext dos <td> | |
// então eu pego 0,2 e 4 ;) | |
echo ($tables[5]->children(0)->children(0)->plaintext . "\t"); // primeiro <td> | |
echo ($tables[5]->children(0)->children(2)->plaintext . "\t"); // segundo <td> | |
echo ($tables[5]->children(0)->children(4)->plaintext . "\t"); // terceiro <td> | |
echo "\n"; | |
foreach ($tables[6]->children() as $child) | |
{ | |
// na tabela 6 é onde contém os valores de Nivel,Atividade e Gestão | |
// $tables[6]->children() retorna um array com os nós filhos | |
// cada nó aqui vai corresponder a um <TR> armazenado em $child | |
// os nós filhos de cada <TR> são 3 <td> N | A | G | |
// aí fica "fácil" ... basta pegar o conteúdo (plaintext) de cada <td> e retornar | |
//-------------------------------------------------------------------------------- | |
echo $child->children(0)->plaintext . " | \t"; // TR[n]->TD[0] - TR = $child | |
echo $child->children(1)->plaintext . " | \t"; // TR[n]->TD[1] | |
echo $child->children(2)->plaintext . " | \t"; // TR[n]->TD[2] | |
echo "\n"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment