Last active
January 15, 2021 07:14
-
-
Save Lysak/851973e1bebc76767df7f54ac5552cd2 to your computer and use it in GitHub Desktop.
yii2 command for grab and parse html
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 | |
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