Created
June 23, 2024 05:04
-
-
Save myselfshravan/67da767195b4abe9fdaaf38939c704ff to your computer and use it in GitHub Desktop.
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
import puppeteer from "puppeteer"; | |
async function scrape() { | |
// Launch the browser | |
const browser = await puppeteer.launch({ headless: false }); | |
const page = await browser.newPage(); | |
// Navigate to the URL | |
await page.goto("https://parents.msrit.edu/newparents/"); | |
await page.setViewport({ width: 1080, height: 1024 }); | |
async function fillForm(page, usn, day, month, year) { | |
await page.evaluate( | |
({ usn, day, month, year }) => { | |
function triggerEvent(element, eventType) { | |
const event = new Event(eventType, { | |
bubbles: true, | |
cancelable: true, | |
}); | |
element.dispatchEvent(event); | |
} | |
const usernameField = document.getElementById("username"); | |
if (usernameField) { | |
usernameField.value = usn; | |
triggerEvent(usernameField, "input"); | |
} | |
const ddField = document.getElementById("dd"); | |
const mmField = document.getElementById("mm"); | |
const yyyyField = document.getElementById("yyyy"); | |
if (ddField && mmField && yyyyField) { | |
ddField.value = day.padStart(2, "0") + " "; | |
if (ddField.value !== day.padStart(2, "0") + " ") { | |
ddField.value = day.padStart(2, "0"); | |
} | |
if (ddField.value === "") { | |
ddField.selectedIndex = parseInt(day); | |
} | |
triggerEvent(ddField, "change"); | |
mmField.value = month.padStart(2, "0"); | |
triggerEvent(mmField, "change"); | |
yyyyField.value = year; | |
triggerEvent(yyyyField, "change"); | |
} | |
if (typeof putdate === "function") { | |
putdate(); | |
} | |
setTimeout(() => { | |
const form = document.getElementById("login-form"); | |
if (form) { | |
form.submit(); | |
} | |
}, 500); | |
}, | |
{ usn, day, month, year } | |
); | |
} | |
// Fill the form and login | |
await fillForm(page, "1MS21CI049", "21 ", "05", "2003"); | |
await page.waitForNavigation(); | |
// Perform extraction after login | |
const scrapedData = await page.evaluate(() => { | |
const name = document.querySelector("h3").innerText; | |
const courses = []; | |
const rows = document.querySelectorAll("table.cn-pay-table tbody tr"); | |
rows.forEach((row) => { | |
const columns = row.querySelectorAll("td"); | |
if (columns.length >= 5) { | |
const code = columns[0].textContent.trim(); | |
const name = columns[1].textContent.trim(); | |
const attendanceButton = columns[4].querySelector("button"); | |
const cie_url = columns[5].querySelector("a").href; | |
const attendance = attendanceButton | |
? parseInt(attendanceButton.textContent) | |
: 0; | |
courses.push({ code, name, attendance, cie_url }); | |
} | |
}); | |
return { name, courses }; | |
}); | |
// console.log("Scraped Data:", scrapedData); | |
for (let course of scrapedData.courses) { | |
await page.goto(course.cie_url); | |
const finalCIEMarks = await page.evaluate(() => { | |
const finalCIEMarksCell = document.querySelector( | |
"table.uk-table tbody tr td:nth-child(8)" | |
); | |
return finalCIEMarksCell ? finalCIEMarksCell.textContent.trim() : "N/A"; | |
}); | |
course.finalCIEMarks = finalCIEMarks; | |
} | |
console.log("Scraped Data with Final CIE Marks:", scrapedData); | |
await browser.close(); | |
} | |
scrape(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment