Skip to content

Instantly share code, notes, and snippets.

@Lysak
Last active Jan 15, 2021
Embed
What would you like to do?
yii2 command for grab and parse html
<?php
namespace app\commands;
use app\components\CurlClient;
use app\components\UtilsManager;
use DOMDocument;
use Exception;
use yii\console\Controller;
/**
* Class TestController
* @package app\commands
*/
class TestController extends Controller
{
/**
* @throws Exception
*/
public function actionIndex(): void
{
// cron = everyday at 00:00
// */5 * * * * php /var/www/api/yii test/index
$client = new CurlClient();
$result = $client->get("https://miss.moemisto.ua/vn", ['sort_field'=>'ratingTotal']);
$dom = new DomDocument;
@$dom->loadHTML($result);
$dom->preserveWhiteSpace = false;
$divs = $dom->getElementsByTagName('span');
$count = 1;
$res = [];
foreach ($divs as $div) {
$class = $div->getAttribute('class');
if ($class == 'new_plashka pseudo-link' && $count <= 3) {
$res[$count] = $div->textContent;
$count++;
}
}
$first = $res[1] - $res[2];
$res[1] = $res[1] . ' (+' . $first . ')';
UtilsManager::telegramLog($res, '$res', __METHOD__ . ':' . __LINE__);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment