buat folder
npm init -y
npm i puppeteer
buat file index.js paste code:
import puppeteer from "puppeteer";
npm init -y
npm i puppeteer
buat file index.js paste code:
import puppeteer from "puppeteer";
const url = "https://www.joshwcomeau.com/";
const main = async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const allArticles = await page.evaluate(() => {
const articles = document.querySelectorAll("article");
return Array.from(articles)
.slice(0, 3)
.map((article) => {
const title = article.querySelector('h3').innerText;
const url = article.querySelector('a').href;
return { title, url };
});
});
console.log(allArticles);
};
main();
jalankan: node index.js
maka hasilnya:
D:\belajar\webscraping (master) (webscraping@1.0.0)
λ node index.js
[
{
title: 'How To Center a Div',
url: 'https://www.joshwcomeau.com/css/center-a-div/'
},
{
title: 'An Interactive Guide to CSS Grid',
url: 'https://www.joshwcomeau.com/css/interactive-guide-to-grid/'
},
{
title: 'Understanding the JavaScript Modulo Operator',
url: 'https://www.joshwcomeau.com/javascript/modulo-operator/'
}
]
jalankan: node index.js
maka hasilnya:
D:\belajar\webscraping (master) (webscraping@1.0.0)
λ node index.js
[
{
title: 'How To Center a Div',
url: 'https://www.joshwcomeau.com/css/center-a-div/'
},
{
title: 'An Interactive Guide to CSS Grid',
url: 'https://www.joshwcomeau.com/css/interactive-guide-to-grid/'
},
{
title: 'Understanding the JavaScript Modulo Operator',
url: 'https://www.joshwcomeau.com/javascript/modulo-operator/'
}
]