buatkan skrip kode scrape javascript youtube in javascript
index.tsx
// This code snippet shows how to scrape YouTube using JavaScriptconst puppeteer = require('puppeteer');
(async () => {
// Open a headless browserconst browser = await puppeteer.launch();
const page = await browser.newPage();
// Go to the YouTube websiteawait page.goto('https://www.youtube.com/');
// Wait for the search input field to load and type a search queryawait page.waitForSelector('input#search');
await page.type('input#search', 'Your Search Query');
// Wait for the search button and click itawait page.waitForSelector('button#search-icon-legacy');
await page.click('button#search-icon-legacy');
// Wait for the search results to loadawait page.waitForSelector('a#video-title');
// Get the URLs of the search resultsconst videoLinks = await page.evaluate(() => {
const links = Array.from(document.querySelectorAll('a#video-title'));
return links.map(link => link.href);
});
console.log(videoLinks);
// Close the browserawait browser.close();
})();