Last active
May 2, 2021 17:15
-
-
Save husain-zaidi/61c6ae1539e9d82e4e5ac66140a8783c to your computer and use it in GitHub Desktop.
Scraper for Directorate of Medical & Health Services, Uttar Pradesh: Covid 19 Bed Tracker
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
const cheerio = require('cheerio'); | |
const axios = require('axios'); | |
const createCsvWriter = require('csv-writer').createObjectCsvWriter; | |
const csvWriter = createCsvWriter({ | |
path: 'out.csv', | |
header: [ | |
{id: 'hospitalName', title: 'Hospital Name'}, | |
{id: 'lastUpdate', title: 'Last Update'}, | |
{id: 'address', title: 'Address'}, | |
{id: 'number', title: 'Phone Number'}, | |
{id: 'totalBeds', title: 'Total Beds'}, | |
{id: 'availableBeds', title: 'Available Beds'}, | |
] | |
}); | |
async function getData(){ | |
// get the site | |
const response = await axios.get('https://beds.dgmhup-covid19.in/EN/covid19bedtrack') | |
.catch(function (error) { | |
// handle error | |
console.log(error); | |
}) | |
// extract the hospitals | |
const $ = cheerio.load(response.data); | |
const hospitals = $('#MainContent_EN_UpdatePanel1').find($('span')) | |
const beds = $('#MainContent_EN_UpdatePanel1').find($('a')) | |
// fill the spreadsheet | |
const data = [] | |
var j = 0 | |
for (let i = 0; i < beds.length; j++, i++ ) { | |
var entry = {} | |
entry.hospitalName = hospitals.eq(j).text().trim() | |
j += 2 | |
entry.address = hospitals.eq(j).text().trim() | |
j++ | |
entry.lastUpdate = hospitals.eq(j).text().trim() | |
j++ | |
entry.number = hospitals.eq(j).text().trim() | |
entry.totalBeds = beds.eq(i).text().trim() | |
i++ | |
entry.availableBeds = beds.eq(i).text().trim() | |
data.push(entry) | |
} | |
csvWriter | |
.writeRecords(data) | |
.then(()=> console.log('The CSV file was written successfully')); | |
} | |
getData() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment