buatkan skrip kode scrape javascript youtube in javascript

index.tsx
// This code snippet shows how to scrape YouTube using JavaScript

const puppeteer = require('puppeteer');

(async () => {
  // Open a headless browser
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Go to the YouTube website
  await page.goto('https://www.youtube.com/');

  // Wait for the search input field to load and type a search query
  await page.waitForSelector('input#search');
  await page.type('input#search', 'Your Search Query');

  // Wait for the search button and click it
  await page.waitForSelector('button#search-icon-legacy');
  await page.click('button#search-icon-legacy');

  // Wait for the search results to load
  await page.waitForSelector('a#video-title');

  // Get the URLs of the search results
  const videoLinks = await page.evaluate(() => {
    const links = Array.from(document.querySelectorAll('a#video-title'));
    return links.map(link => link.href);
  });

  console.log(videoLinks);

  // Close the browser
  await browser.close();
})();
1024 chars
35 lines

related categories

gistlibby LogSnag