Skip to content

Instantly share code, notes, and snippets.

@iamspark1e
Last active June 30, 2022 03:47
Show Gist options
  • Save iamspark1e/8f25c024866c626494b73ac3852dbe85 to your computer and use it in GitHub Desktop.
Save iamspark1e/8f25c024866c626494b73ac3852dbe85 to your computer and use it in GitHub Desktop.
const express = require('express'); // Adding Express
const app = express(); // Initializing Express
const puppeteer = require('puppeteer'); // Adding Puppeteer, Run `apt-get install chromium` first (on Debian)
// Wrapping the Puppeteer browser logic in a GET request
app.get('/', function (req, res) {
// Launching the Puppeteer controlled headless browser and navigate to the Digimon website
puppeteer.launch({
headless: true,
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--no-first-run',
'--no-sandbox',
'--no-zygote',
'--single-process'
],
executablePath: '/usr/bin/chromium' // Got by running `which chromium`
}).then(async function (browser) {
const page = await browser.newPage();
await page.goto('http://digidb.io/digimon-list/');
// Targeting the DOM Nodes that contain the Digimon names
const digimonNames = await page.$$eval('#digiList tbody tr td:nth-child(2) a', function (digimons) {
// Mapping each Digimon name to an array
return digimons.map(function (digimon) {
return digimon.innerText;
});
});
// Closing the Puppeteer controlled headless browser
await browser.close();
// Sending the Digimon names to Postman
res.send(digimonNames);
});
});
// Making Express listen on port 7000
app.listen(7000, function () {
console.log('Running on port 7000.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment