Created
June 6, 2023 05:45
-
-
Save fazt/497cbe26ef9f6ca038b4ea188ff048d3 to your computer and use it in GitHub Desktop.
This file contains 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 openWebPage() { | |
const browser = await puppeteer.launch({ | |
headless: false, | |
slowMo: 200, | |
}); | |
const page = await browser.newPage(); | |
await page.goto("https://www.example.com"); | |
await browser.close(); | |
} | |
// openWebPage(); | |
async function captureScreenshot() { | |
const browser = await puppeteer.launch({ | |
headless: 'new' | |
}); | |
const page = await browser.newPage(); | |
await page.goto('https://www.example.com'); | |
await page.screenshot({path: 'example.png'}); | |
await browser.close(); | |
} | |
captureScreenshot(); | |
async function navigateWebPage() { | |
const browser = await puppeteer.launch({ | |
slowMo: 200, | |
headless: false, | |
}); | |
const page = await browser.newPage(); | |
await page.goto("https://quotes.toscrape.com"); | |
await page.click('a[href="/login"]'); | |
await new Promise((resolve) => setTimeout(resolve, 3000)); | |
await browser.close(); | |
} | |
// navigateWebPage(); | |
async function getDataFromWebPage() { | |
const browser = await puppeteer.launch({ | |
slowMo: 200, | |
headless: false, | |
}); | |
const page = await browser.newPage(); | |
await page.goto("https://www.example.com"); | |
const data = await page.evaluate(() => { | |
let title = document.querySelector("h1").innerText; | |
let description = document.querySelector("p").innerText; | |
return { | |
title, | |
description, | |
}; | |
}); | |
console.log(data); | |
await browser.close(); | |
} | |
// getDataFromWebPage(); | |
async function handleDynamicWebPage() { | |
const browser = await puppeteer.launch({ | |
headless: false, | |
slowMo: 200, | |
}); | |
const page = await browser.newPage(); | |
await page.goto("https://quotes.toscrape.com"); | |
// await page.waitForSelector('div[data-loaded="true"]'); // Asegúrate de reemplazar esto con el selector de CSS correcto. | |
const data = await page.evaluate(() => { | |
const quotes = document.querySelectorAll(".quote"); | |
const data = [...quotes].map((quote) => { | |
const quoteText = quote.querySelector(".text").innerText; | |
const author = quote.querySelector(".author").innerText; | |
const tags = [...quote.querySelectorAll(".tag")].map( | |
(tag) => tag.innerText | |
); | |
return { | |
quoteText, | |
author, | |
tags, | |
}; | |
}); | |
return data; | |
}); | |
console.log(data); | |
await browser.close(); | |
} | |
// handleDynamicWebPage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hace tiempo que te sigo y no me explico como haces para saber tanto, estoy dudando que seas de este planeta...