Skip to content

Instantly share code, notes, and snippets.

@LearnWebCode
Created July 21, 2021 23:41
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save LearnWebCode/31f8a20ef7e4324aca322a21dbfc3d7e to your computer and use it in GitHub Desktop.
Save LearnWebCode/31f8a20ef7e4324aca322a21dbfc3d7e to your computer and use it in GitHub Desktop.
Puppeteer / Node.js Automation & Web Scraping Tutorial from YouTube
// in a new folder be sure to run "npm init -y" and "npm install puppeteer"
const puppeteer = require("puppeteer")
const fs = require("fs/promises")
async function start() {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto("https://learnwebcode.github.io/practice-requests/")
const names = await page.evaluate(() => {
return Array.from(document.querySelectorAll(".info strong")).map(x => x.textContent)
})
await fs.writeFile("names.txt", names.join("\r\n"))
await page.click("#clickme")
const clickedData = await page.$eval("#data", el => el.textContent)
console.log(clickedData)
const photos = await page.$$eval("img", imgs => {
return imgs.map(x => x.src)
})
await page.type("#ourfield", "blue")
await Promise.all([page.click("#ourform button"), page.waitForNavigation()])
const info = await page.$eval("#message", el => el.textContent)
console.log(info)
for (const photo of photos) {
const imagepage = await page.goto(photo)
await fs.writeFile(photo.split("/").pop(), await imagepage.buffer())
}
await browser.close()
}
start()
@ghayyas009
Copy link

thanks u sir . your youtube lecture clear my lot of queries.............thanks once again.

@ThejasRP
Copy link

Great! Thanks!

@lisawebcoder
Copy link

(node:9588) UnhandledPromiseRejectionWarning: TimeoutError: Timed out after 30000 ms while trying to connect to the browser! Only Chrome at revision r938248 is guaranteed to work.
at Timeout.onTimeout (C:\Users\Main\Desktop\example\node_modules\puppeteer\lib\cjs\puppeteer\node\BrowserRunner.js:240:20)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
(Use node --trace-warnings ... to show where the warning was created)
(node:9588) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9588) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment