Skip to content

Instantly share code, notes, and snippets.

@fazt
Created June 6, 2023 05:45
Show Gist options
  • Save fazt/497cbe26ef9f6ca038b4ea188ff048d3 to your computer and use it in GitHub Desktop.
Save fazt/497cbe26ef9f6ca038b4ea188ff048d3 to your computer and use it in GitHub Desktop.
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();
@GuilleSalinas
Copy link

hace tiempo que te sigo y no me explico como haces para saber tanto, estoy dudando que seas de este planeta...

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