Skip to content

Instantly share code, notes, and snippets.

@Lysak
Last active January 15, 2021 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lysak/851973e1bebc76767df7f54ac5552cd2 to your computer and use it in GitHub Desktop.
Save Lysak/851973e1bebc76767df7f54ac5552cd2 to your computer and use it in GitHub Desktop.
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