From 1f96b88ac2657863df66e7cb87414dffe59a841e Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 8 Aug 2024 13:32:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D0=B1=D0=B8=D0=BB=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app.php | 35 +++++ app/library/Database.php | 64 ++++++++ Size.php => app/library/Size.php | 0 .../library/Specialization.php | 16 +- app/library/SvedenParser.php | 62 ++++++++ composer.json | 6 + requests.sql => create_table.sql | 0 index.php | 4 +- log/log.php | 4 + log/php-error.log | 7 + parser.php | 144 ------------------ vendor/composer/autoload_psr4.php | 4 +- vendor/composer/autoload_static.php | 17 ++- 13 files changed, 205 insertions(+), 158 deletions(-) create mode 100644 app/app.php create mode 100644 app/library/Database.php rename Size.php => app/library/Size.php (100%) rename Specialization.php => app/library/Specialization.php (75%) create mode 100644 app/library/SvedenParser.php rename requests.sql => create_table.sql (100%) create mode 100644 log/log.php create mode 100644 log/php-error.log delete mode 100755 parser.php diff --git a/app/app.php b/app/app.php new file mode 100644 index 0000000..f89c77d --- /dev/null +++ b/app/app.php @@ -0,0 +1,35 @@ + "10.90.1.201", + "database" => "opendata", + "user" => "niimko_user", + "password" => "MOhA17FeboXE" +]; + +$client = new Client(); +$response = $client->get('https://marsu.ru/sveden/education/eduChislen.php'); +$html = $response->getBody()->getContents(); + +$parser = new SvedenParser($html, '//tr[@itemprop="eduChislen"]//'); +$data = $parser->getDataTable(); + +$db = new Database( + "mysql:host={$dbconfig['host']};dbname={$dbconfig['database']}", + $dbconfig['user'], + $dbconfig['password'] +); + +// print_r($data); + +// $db->insert('sveden_table_education', $data); +$data = $db->select('sveden_education_contingent'); + +echo "
";
+print_r($data);
+echo "
"; \ No newline at end of file diff --git a/app/library/Database.php b/app/library/Database.php new file mode 100644 index 0000000..b0bac0c --- /dev/null +++ b/app/library/Database.php @@ -0,0 +1,64 @@ + PDO::ERRMODE_EXCEPTION] + ); + echo "Подлючено успешно!\n"; + } catch (PDOException $e) { + echo "Ошибка подключения:". $e->getMessage() . "\n"; + } + } + + public function __destruct() + { + echo "Подключение прервано!\n"; + } + + public function insert(string $table, array $data) : void + { + $stmt = $this->prepare(" + INSERT INTO sveden_table_education + (spec_code, spec_name, edu_forms, edu_level, contingent) + VALUES + (:spec_code, :spec_name, :edu_forms, :edu_level, :contingent)" + ); + foreach ($data as $row) { + try { + $stmt->bindParam(':spec_code', $row['eduCode']); + $stmt->bindParam(':spec_name', $row['eduName']); + $stmt->bindParam(':edu_forms', $row['edoForms']); + $stmt->bindParam(':edu_level', $row['eduLevel']); + $stmt->bindParam(':contingent', $row['contingent']); + + $stmt->execute(); + } catch (PDOException $e) { + echo "Ошибка запроса: " . $e->getMessage() . "\n"; + } + } + } + + public function select(string $table) : array + { + $stmt = $this->prepare("SELECT * FROM $table"); + try { + $stmt->execute(); + $array = $stmt->fetchAll(PDO::FETCH_ASSOC); + } catch (PDOException $e) { + echo "Ошибка запроса: " . $e->getMessage() . "\n"; + } finally { + return $array; + } + } +} \ No newline at end of file diff --git a/Size.php b/app/library/Size.php similarity index 100% rename from Size.php rename to app/library/Size.php diff --git a/Specialization.php b/app/library/Specialization.php similarity index 75% rename from Specialization.php rename to app/library/Specialization.php index d74a440..7e30c03 100644 --- a/Specialization.php +++ b/app/library/Specialization.php @@ -1,4 +1,5 @@ eduCode = $eduCode; $this->eduName = $eduName; $this->eduLevel = $eduLevel; $this->eduForm = $eduForm; - // $this->size = $size; $this->contingent = $contingent; } public function getData() : array { return [ - "spec_code" => $this->eduCode, - "spec_name" => $this->eduName, - "edu_level" => $this->eduLevel, - "edu_forms"=> $this->eduForm, - // "size" => $this->size, + "eduCode" => $this->eduCode, + "eduName" => $this->eduName, + "eduLevel" => $this->eduLevel, + "edoForms"=> $this->eduForm, "contingent" => $this->contingent ]; } diff --git a/app/library/SvedenParser.php b/app/library/SvedenParser.php new file mode 100644 index 0000000..3024297 --- /dev/null +++ b/app/library/SvedenParser.php @@ -0,0 +1,62 @@ + "td", + "eduName" => "td", + "eduLevel" => "td", + "eduForm" => "td", + "numberBF" => "th", + "numberBFF" => "th", + "numberBR" => "th", + "numberBRF" => "th", + "numberBM" => "th", + "numberBMF" => "th", + "numberP" => "th", + "numberPF" => "th", + "numberAll" => "th" + ]; + + public function __construct(string $html, string $template) + { + libxml_use_internal_errors(true); + $dom = new \DOMDocument(); + $dom->loadHTML($html); + $this->xpath = new \DOMXPath($dom); + $this->template = $template; + } + + private function parse(): array + { + $data = array(); + foreach (self::FIELDS as $field => $tag) { + $data[$field] = $this->xpath->query($this->template . $tag . "[@itemprop=\"$field\"]"); + } + return $data; + } + + public function getDataTable() : array + { + $data = $this->parse(); + $spec = new Specialization(); + $records = array(); + + for ($i = 0; $i < $data['numberAll']->length; $i++) { + $spec->update( + $data['eduCode']->item($i)->textContent, + $data['eduName']->item($i)->textContent, + $data['eduLevel']->item($i)->textContent, + $data['eduForm']->item($i)->textContent, + (int)$data['numberAll']->item($i)->textContent + ); + $records[] = $spec->getData(); + } + return $records; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index fc6056e..04f79aa 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,11 @@ { "require": { "guzzlehttp/guzzle": "^7.0" + }, + "autoload": { + "psr-4": { + "App\\": "app/", + "App\\Library\\": "app/library" + } } } diff --git a/requests.sql b/create_table.sql similarity index 100% rename from requests.sql rename to create_table.sql diff --git a/index.php b/index.php index eb1ed21..21fae64 100644 --- a/index.php +++ b/index.php @@ -1,2 +1,4 @@ select() +#1 /home/developer/sp/index.php(4): require_once('...') +#2 {main} + thrown in /home/developer/sp/app/library/Database.php on line 61 diff --git a/parser.php b/parser.php deleted file mode 100755 index 4fb47e0..0000000 --- a/parser.php +++ /dev/null @@ -1,144 +0,0 @@ - '10.90.1.201', - 'database' => 'opendata', - 'user' => 'niimko_user', - 'password' => 'MOhA17FeboXE' -]; - -$fields = [ - 'eduCode' => 'td', - 'eduName' => 'td', - 'eduLevel' => 'td', - 'eduForm' => 'td', - // 'numberBF' => 'th', - // 'numberBFF' => 'th', - // 'numberBR' => 'th', - // 'numberBRF' => 'th', - // 'numberBM' => 'th', - // 'numberBMF' => 'th', - // 'numberP' => 'th', - // 'numberPF' => 'th', - 'numberAll' => 'th', -]; -$data = []; - -$client = new Client(); -$response = $client->get('https://marsu.ru/sveden/education/eduChislen.php'); -$html = $response->getBody()->getContents(); -libxml_use_internal_errors(true); - -$dom = new DOMDocument(); -$dom->loadHTML($html); -$xpath = new DOMXPath($dom); - -foreach ($fields as $field => $tag) { - $data[$field] = $xpath->query(TEMPLATE . $tag . "[@itemprop=\"$field\"]"); -} - -// echo "
";
-// print_r($data['numberAll'][0]);
-// echo "
"; - -$specialization = new Specialization(); -// $sizeFederalBudget = new Size(); -// $sizeRussianBudget = new Size(); -// $sizeLocalBudget = new Size(); -// $sizeIndividualsOrLegalEntitiesBudget = new Size(); -$records = []; -$size = []; - -for ($i = 0; $i < $data['numberAll']->length; $i++) { - // $sizeFederalBudget->update( - // $data['numberBF']->item($i)->textContent, - // $data['numberBFF']->item($i)->textContent - // ); - // $size['бюджетных ассигнований федерального бюджета'] = $sizeFederalBudget->getData(); - - // $sizeRussianBudget->update( - // $data['numberBR']->item($i)->textContent, - // $data['numberBRF']->item($i)->textContent - // ); - // $size['бюджетов субъектов Российской Федерации'] = $sizeRussianBudget->getData(); - - // $sizeLocalBudget->update( - // $data['numberBM']->item($i)->textContent, - // $data['numberBMF']->item($i)->textContent - // ); - // $size['местных бюджетов'] = $sizeLocalBudget->getData(); - - // $sizeIndividualsOrLegalEntitiesBudget->update( - // $data['numberP']->item($i)->textContent, - // $data['numberPF']->item($i)->textContent - // ); - // $size['средств физических и (или) юридических лиц'] = $sizeIndividualsOrLegalEntitiesBudget->getData(); - - $specialization->update( - $data['eduCode']->item($i)->textContent, - $data['eduName']->item($i)->textContent, - $data['eduLevel']->item($i)->textContent, - $data['eduForm']->item($i)->textContent, - // [], - (int)$data['numberAll']->item($i)->textContent - ); - $records[] = $specialization->getData(); - $size = []; -} - -// echo "
";
-// print_r($records[0]);
-// echo "
"; - -try { - $pdo = new PDO( - "mysql:host={$dbconfig['host']};dbname={$dbconfig['database']}", - $dbconfig['user'], - $dbconfig['password'] - ); - $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - echo "Подключение успешно!"; -} catch (PDOException $e) { - echo "Ошибка подключения: " . $e->getMessage() . "\n"; -} - -$sql = " - INSERT - INTO sveden_education_contingent ( - spec_code, - spec_name, - edu_level, - edu_forms, - contingent - ) VALUES ( - :spec_code, - :spec_name, - :edu_level, - :edu_forms, - :contingent - ) -"; - -// Почему-то не сработала -// foreach ($records as $record) { -// $sth = $pdo->prepare($sql); -// try { -// $sth->execute($record); -// } catch (PDOException $e) { -// echo "Ошибка выполнения запроса:". $e->getMessage() . ""; -// } -// } - -$sql = 'SELECT * FROM sveden_education_contingent'; -$array = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC); - -echo "
";
-print_r($array);
-echo "
"; \ No newline at end of file diff --git a/vendor/composer/autoload_psr4.php b/vendor/composer/autoload_psr4.php index 61392dc..63d5de3 100644 --- a/vendor/composer/autoload_psr4.php +++ b/vendor/composer/autoload_psr4.php @@ -6,9 +6,11 @@ $vendorDir = dirname(__DIR__); $baseDir = dirname($vendorDir); return array( - 'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-message/src', $vendorDir . '/psr/http-factory/src'), + 'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-factory/src', $vendorDir . '/psr/http-message/src'), 'Psr\\Http\\Client\\' => array($vendorDir . '/psr/http-client/src'), 'GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'), 'GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'), 'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'), + 'App\\Library\\' => array($baseDir . '/app/library'), + 'App\\' => array($baseDir . '/app'), ); diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index d7a3698..b5198f5 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -24,13 +24,18 @@ class ComposerStaticInit045658d81f6d9d3243e731dda7bf04d1 'GuzzleHttp\\Promise\\' => 19, 'GuzzleHttp\\' => 11, ), + 'A' => + array ( + 'App\\Library\\' => 12, + 'App\\' => 4, + ), ); public static $prefixDirsPsr4 = array ( 'Psr\\Http\\Message\\' => array ( - 0 => __DIR__ . '/..' . '/psr/http-message/src', - 1 => __DIR__ . '/..' . '/psr/http-factory/src', + 0 => __DIR__ . '/..' . '/psr/http-factory/src', + 1 => __DIR__ . '/..' . '/psr/http-message/src', ), 'Psr\\Http\\Client\\' => array ( @@ -48,6 +53,14 @@ class ComposerStaticInit045658d81f6d9d3243e731dda7bf04d1 array ( 0 => __DIR__ . '/..' . '/guzzlehttp/guzzle/src', ), + 'App\\Library\\' => + array ( + 0 => __DIR__ . '/../..' . '/app/library', + ), + 'App\\' => + array ( + 0 => __DIR__ . '/../..' . '/app', + ), ); public static $classMap = array (