Breaking

Web Scraping with PHP - Create a COVID-19 Statistics Website By Scraping Other Website

 Web scraping, web harvesting, or web data extraction is data scraping used for extracting data from websites. The web scraping software may directly access the World Wide Web using the Hypertext Transfer Protocol or a web browser. If you want to learn more you can check out my youtube channel.



index.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, 
        maximum-scale=1.0, minimum-scale=1.0"> 
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Web - Scraping</title>
    <link rel="stylesheet" 
       href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css">
</head>
<body>
<div class="ui menu blue">
    <div class="ui container">
        <li class="item"><a href="#">Home</a></li>
        <li class="item"><a href="#">About</a></li>
        <li class="item"><a href="#">Contact</a></li>
    </div>
</div>

<br>

<?php
//INITILIZE CURL
$ch = curl_init();

//SETTING OPTIONS FOR CURL
curl_setopt($ch, CURLOPT_URL,"https://corona.gov.bd/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
//true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.

$html = curl_exec($ch);


//echo $html;
//exit();

$dom = new DOMDocument();
@ $dom->loadHTML($html);


$h3s = $dom->getElementsByTagName('h3');
$h1s = $dom->getElementsByTagName('h1');





/*
//echo "<pre />";
//print_r($h4s[0]);

//print_r($h4s);
$h4_array = array();
foreach ($h4s as $h4){
//    echo $h4->textContent;
//    print_r($h4);
    $title = $h4->textContent;
    $h4_array[] = $title;
}


echo "<pre />";
print_r ( utf8_decode( $h4_array[0]));
*/


?>


<div class="ui container">
    <div class="ui grid">
        <div class="four column row">
            <div class="column">
                <div class="ui segment blue">
                    <h2 class="ui header dividing blue">New Affected</h2>
                    <div class="ui two column grid">
                        <div class="column">
                            <h4 class="ui header green">Last 24 hours</h4>
                            <h3 class="ui header blue">
                               <?php echo utf8_decode( $h3s[1]->textContent ); ?>
                            </h3>
                        </div>
                        <div class="column">
                            <h4 class="ui header green">Total</h4>
                            <h3 class="ui header blue">
                               <?php echo utf8_decode( $h3s[2]->textContent ); ?>
                            </h3>
                        </div>
                    </div>
                </div>
            </div>
            <div class="column">
                <div class="ui segment blue">
                    <h2 class="ui header dividing blue">Death</h2>
                    <div class="ui two column grid">
                        <div class="column">
                            <h4 class="ui header green">Last 24 hours</h4>
                            <h3 class="ui header blue">
                               <?php echo utf8_decode( $h1s[0]->textContent ); ?>
                             </h3>
                        </div>
                        <div class="column">
                            <h4 class="ui header green">Total</h4>
                            <h3 class="ui header blue">
                               <?php echo utf8_decode( $h1s[1]->textContent ); ?>
                            </h3>
                        </div>
                    </div>
                </div>
            </div>
            <div class="column">
                <div class="ui segment blue">
                    <h2 class="ui header dividing blue">Recover</h2>
                    <div class="ui two column grid">
                        <div class="column">
                            <h4 class="ui header green">Last 24 hours</h4>
                            <h3 class="ui header blue">
                               <?php echo utf8_decode( $h3s[3]->textContent ); ?>
                            </h3>
                        </div>
                        <div class="column">
                            <h4 class="ui header green">Total</h4>
                            <h3 class="ui header blue">
                                <?php echo utf8_decode( $h3s[4]->textContent ); ?>
                            </h3>
                        </div>
                    </div>
                </div>
            </div>
            <div class="column">
                <div class="ui segment blue">
                    <h2 class="ui header dividing blue">Test</h2>
                    <div class="ui two column grid">
                        <div class="column">
                            <h4 class="ui header green">Last 24 hours</h4>
                            <h3 class="ui header blue">
                                <?php echo utf8_decode( $h3s[5]->textContent ); ?>
                            </h3>
                        </div>
                        <div class="column">
                            <h4 class="ui header green">Total</h4>
                            <h3 class="ui header blue">
                                <?php echo utf8_decode( $h3s[6]->textContent ); ?>
                            </h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



</body>
</html>





No comments:

Powered by Blogger.