Build a Covid website using HTML, PHP and Python
Building a Covid update website using HTML, PHP, and Python
During this pandemic, people get scared and stressed from seeing the covid cases reported. Besides this people show interest in looking at the daily cases reported, surfing the internet for the exact report.
So a website with an accurate covid report is the most demanded one, nowadays.
To create a website that shows covid cases, we can use HTML for frontend and python, PHP for backend.
Python is the most popular programming language. Just learning the theoretical concepts alone will not make us gain knowledge. So it is very important to apply it in real-time applications for better understanding. Try yourself to implement what you think. No matter if you don't arrive at the right solution. Keep trying. Trial and error make you learn more.
Here I have used the Web scraping technique to get the number of covid cases. Web scraping is the concept of extracting data from websites. For this application, I have extracted the data from worldometers website.
Pre-requisites :
1. Python - Click here to know how to install python.
2. Wamp server
For running the PHP file we need a wamp server (for windows), lamp server (for Linux), Xampp(for all OS).
Click here to download the Wamp server.
Click here to download the Lamp server.
Click here to download the Xampp server.
3. Python modules
Install the required python modules by executing the following commands in the command prompt.
pip install bs4
pip install tabulate
pip install matplotlib
pip install NumPy
pip install requests
The frontend and backend code is given below.
Corona_case.py :
# Import required module
import requests
import bs4
import json
# Make requests from webpage
result= requests.get('https://www.worldometers.info/coronavirus/country/india/')
# Creating soap object
soup = bs4.BeautifulSoup(result.text,'lxml')
# Searching div tags having maincounter-number class
cases = soup.find_all('div' ,class_= 'maincounter-number')
# List to store number of cases
data = []
# Find the span and get data from it
for i in cases:
span = i.find('span')
data.append(span.string)
# Dispaly number of cases
print(data)
with open('corona_data.txt', 'w') as f:
for i in data:
f.write(str(i)+"\n")
import pandas as pd
# Creating dataframe
df = pd.DataFrame({"CoronaData": data})
# Naming the coloumns
df.index = ['TotalCases', ' Deaths', 'Recovered']
# Exporing data into Excel
df.to_csv('Corona_Data.csv')
Corona_case.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Corona updates</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
}
/* Style the header */
.header {
background-color: RebeccaPurple ;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="header">
<h1>COVID UPDATES</h1>
</div>
<center><?php include 'file_data.php';?></center>
</body>
</html>
File_data.php
<?php
$filename = "corona_data.txt";
$title = array("<h2 style= color:blue><b>Total cases</b></h2>","<h2 style= color:red><b>Death</b></h2>", "<h2 style= color:green><b>Recovered</b></h2>");
$data = array();
$file= fopen( $filename, "r" );
$i=0;
while(!feof($file) && $i<3)
{
$line=fgets($file);
array_push($data,"line");
echo "$title[$i]";
echo "<h2>$line</h2>";
$i = $i + 1;
}
?>
How to run the above code?
Run the python file, so that the covid data will be stored in an excel file.
Type “localhost/filename.php”
Now the PHP file will be executed.
Output Screenshot :
Comments
Post a Comment