Last active
September 15, 2022 06:04
-
-
Save SarahElson/79e16f915a12e7ce15cd0aa310b5e8a9 to your computer and use it in GitHub Desktop.
How To Perform Web Scraping With JavaScript And Selenium
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 express = require('express'); | |
const { Builder, By } = require('selenium-webdriver'); | |
const app = express(); | |
const port = 3000; | |
app.get('/', async (request, response) => { | |
// Web Scraping Code here | |
try { | |
const data = await WebScrapingLocalTest(); | |
response.status(200).json(data); | |
} catch (error) { | |
response.status(500).json({ | |
message: 'Server error occurred', | |
}); | |
} | |
}); | |
app.listen(port, () => { | |
console.log(`Example app listening at http://localhost:${port}`); | |
}); | |
async function WebScrapingLocalTest() { | |
try { | |
driver = await new Builder().forBrowser('chrome').build(); | |
await driver.get('https://www.youtube.com/c/LambdaTest/videos'); | |
const allVideos = await driver.findElements( | |
By.css('ytd-grid-video-renderer.style-scope.ytd-grid-renderer') | |
); | |
return await getVideos(allVideos); | |
} catch (error) { | |
throw new Error(error); | |
} finally { | |
await driver.quit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment