Skip to content

Instantly share code, notes, and snippets.

@chongkan
Last active June 5, 2018 00:31
Show Gist options
  • Save chongkan/7b1cfac2ee54f50d5713aac5232f0650 to your computer and use it in GitHub Desktop.
Save chongkan/7b1cfac2ee54f50d5713aac5232f0650 to your computer and use it in GitHub Desktop.
Diputadas y Diputados de Costa Rica en Formato JSON
<?php
include('simple_html_dom.php');
$diputadosCostaRica = new DiputadosCostaRica();
$diputadosCostaRica->index();
class DiputadosCostaRica {
protected $_minChars = 100;
public function index() {
// Create DOM from URL or file
$html = file_get_html('http://www.asamblea.go.cr/ca/lists/diputados/diputadas%20y%20diputados.aspx');
$diputados = array();
// Find the main table
foreach ($html->find('table') as $element) {
if (isset($element->summary) && trim($element->summary) === 'Diputadas y diputados') {
foreach ($element->find('tr') as $tr) {
$persona = new STDClass();
// Foto
if (is_object($tr->find('td', 0))){
$persona->foto = 'http://www.asamblea.go.cr' . $tr->find('td', 0)->find('img', 0)->src;
}
// Email
if (is_object($tr->find('td', 1))){
$persona->email = $tr->find('td', 1)->find('a', 0)->plaintext;
}
// Nombre
if (is_object($tr->find('td', 2))){
$persona->nombre = htmlspecialchars_decode($tr->find('td', 2)->plaintext);
}
// Provincia
if (is_object($tr->find('td', 3))){
$persona->provincia = $tr->find('td', 3)->plaintext;;
}
// Fraccion
if (is_object($tr->find('td', 4))){
$persona->fraccion = $tr->find('td', 4)->plaintext;;
}
if (isset($persona->email)){
$diputados[] = $persona;
}
} // End foreach TR.
} // End foreach Content Table (Summary)
}// En foreach Table.
echo json_encode($diputados);
// TODO
// Store in DB.
// Serve from DB.
// Only store latest additions.
} // End of Index Function.
} // End of Class.
@chongkan
Copy link
Author

diputados_json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment