Last active
August 13, 2020 16:55
-
-
Save xsbr/b586d9f56c0e29c9c12fbb907b535bec to your computer and use it in GitHub Desktop.
Extrai do HTML um JSON com as taxas de anúncios do MercadoLivre por Categoria
This file contains 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 | |
$html = file('https://www.mercadolivre.com.br/landing/costos-venta-producto'); | |
$find = 'window.__PRELOADED_STATE__ = '; | |
$json = false; | |
foreach($html as $row) { | |
$row = trim($row); | |
$pos = strpos($row, $find); | |
if($pos !== false) { | |
$json = @json_decode(substr($row, $pos+strlen($find), -1)); | |
break; | |
} | |
} | |
if(!$json || empty($json->ML->categoriesFromFile)) { | |
die("ERRO: json nao encontrado no HTML"); | |
} | |
$data = $json->ML->categoriesFromFile; | |
print "category_id;classico;premium;domain;descricao\n"; | |
foreach($data as $row) { | |
if(!isset($row->Clasica) || !isset($row->Premium)) { | |
continue; | |
} | |
$categories = array(); | |
$last = false; | |
for($i = 1; $i <= 7; $i++) { | |
$ln = 'L'.$i; | |
$ls = $ln.'_N'; | |
if(isset($row->$ln)) { | |
$categories[] = $row->$ls; | |
$last = $row->$ln; | |
} else { | |
break; | |
} | |
} | |
if($last) { | |
printf("MLB%d;%.2f;%.2f;\"%s\";\"%s\"\n", $last, str_replace(',', '.', $row->Clasica), str_replace(',', '.', $row->Premium), $row->Domain, join('|',$categories)); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment