Hướng dẫn crawl dữ liệu covid 19 bằng ngôn ngữ PHP
Dưới đây là đoạn code nhằm fix lỗi warning ssl của trình duyệt
function file_get_contents_ssl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); // 3 sec.
curl_setopt($ch, CURLOPT_TIMEOUT, 10000); // 10 sec.
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Tiếp theo sao chép trang website thống kê dữ liệu mà bạn muốn crawl data
$html = file_get_contents_ssl('https://ncov.moh.gov.vn/trang-chu');
Get dữ liệu DOMDocument
$DOM = new DOMDocument();
Xem Thêm : Hướng dẫn cài đặt theme TikTok miễn phí tại VNCODE
Copy tiếp đoạn Load html
libxml_use_internal_errors(true);
$DOM ->loadHTML('<?xml encoding="UTF-8">' . $html);
libxml_clear_errors();
Lấy dữ liệu trong table có thẻ th và td
Header = $DOM->getElementsByTagName('th');
$Detail = $DOM->getElementsByTagName('td');
Dùng vòng lặp để get dữ liệu trong table
foreach($Header as $NodeHeader)
{
$aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
}
$i = 0;
$j = 0;
foreach($Detail as $sNodeDetail)
{
$aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
$i = $i + 1;
$j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
}
for($i = 0; $i < count($aDataTableDetailHTML); $i++)
{
for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
{
$aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
}
}
$aDataTableDetailHTML = $aTempData; unset($aTempData);
Sau khi lấy được mảng dữ liệu, tiếp theo mình sẽ get dữ liệu trong mảng $aDataTableDetailHTML hiển thị trên website
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<style>
</style>
</head>
<body>
<div class="container">
<div class="table-wrapper-scroll-y my-custom-scrollbar">
<table class="table table-bordered table-striped mb-0">
<thead>
<tr style="position: sticky;top: 0px;z-index: 1;background: #f2f2f2;">
<th scope="col">STT</th>
<th scope="col">Tỉnh/TP</th>
<th scope="col">Tổng số ca</th>
<th scope="col">Hôm nay</th>
<th scope="col">Tử vong</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"></th>
<td></td>
<td></td>
<td style="color:red;"></td>
<td></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</body>
</html>
Xem Thêm : Hướng dẫn cài đặt sourcode share video
css 1 chút cho table được đẹp hơn
.my-custom-scrollbar {
position: relative;
height: 300px;
overflow: auto;
}
.table-wrapper-scroll-y {
display: block;
}
Gọi dữ liệu ra HTML vào thẻ <tboy> bằng cú pháp PHP
<tbody>
<?php foreach($aDataTableDetailHTML as $key => $value) : ?>
<tr>
<th scope="row"><?= $key ?></th>
<td><?= $value['Tỉnh/TP'] ?></td>
<td><?= $value['Tổng số ca'] ?></td>
<td style="color:red;"><?= $value['Hôm nay'] ?></td>
<td><?= $value['Tử vong'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
Dưới đây là tổng hợp code của bài viết:
<?php
function file_get_contents_ssl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); // 3 sec.
curl_setopt($ch, CURLOPT_TIMEOUT, 10000); // 10 sec.
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$html = file_get_contents_ssl('https://ncov.moh.gov.vn/trang-chu');
$DOM = new DOMDocument();
libxml_use_internal_errors(true);
$DOM ->loadHTML('<?xml encoding="UTF-8">' . $html);
libxml_clear_errors();
$Header = $DOM->getElementsByTagName('th');
$Detail = $DOM->getElementsByTagName('td');
foreach($Header as $NodeHeader)
{
$aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
}
$i = 0;
$j = 0;
foreach($Detail as $sNodeDetail)
{
$aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
$i = $i + 1;
$j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
}
for($i = 0; $i < count($aDataTableDetailHTML); $i++)
{
for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
{
$aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
}
}
$aDataTableDetailHTML = $aTempData; unset($aTempData);
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<style>
.my-custom-scrollbar {
position: relative;
height: 300px;
overflow: auto;
}
.table-wrapper-scroll-y {
display: block;
}
</style>
</head>
<body>
<div class="container">
<div class="table-wrapper-scroll-y my-custom-scrollbar">
<table class="table table-bordered table-striped mb-0">
<thead>
<tr style="position: sticky;top: 0px;z-index: 1;background: #f2f2f2;">
<th scope="col">STT</th>
<th scope="col">Tỉnh/TP</th>
<th scope="col">Tổng số ca</th>
<th scope="col">Hôm nay</th>
<th scope="col">Tử vong</th>
</tr>
</thead>
<tbody>
<?php foreach($aDataTableDetailHTML as $key => $value) : ?>
<tr>
<th scope="row"><?= $key ?></th>
<td><?= $value['Tỉnh/TP'] ?></td>
<td><?= $value['Tổng số ca'] ?></td>
<td style="color:red;"><?= $value['Hôm nay'] ?></td>
<td><?= $value['Tử vong'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</body>
</html>
Trải nghiệm luôn và ngay để được trải nghiệm thêm nhiều cái mới mẻ về PHP nào. Nếu trong quá trình chạy xảy ra lỗi vui lòng comment xuống bài viêt để nhận được sự trợ giúp nhiều hơn từ cộng đồng. Và đừng quên like và share bài viết để hỗ trợ VNCODE đi xa hơn nhé.
Nguồn: https://vncode.info
Danh mục: PHP