Berikut adalah contoh kode untuk melakukan web scraping pada website getimg.ai menggunakan library Cheerio di Node.js:

const axios = require('axios');
const cheerio = require('cheerio');

const url = 'https://getimg.ai/';

// Fungsi untuk melakukan scraping
const scrapeWebsite = async () => {
  try {
    const response = await axios.get(url);
    const $ = cheerio.load(response.data);

    // Contoh scraping data judul artikel
    const articleTitles = [];
    $('.article-title').each((index, element) => {
      const title = $(element).text();
      articleTitles.push(title);
    });

    console.log(articleTitles);
  } catch (error) {
    console.error(error);
  }
};

scrapeWebsite();

Kode di atas akan melakukan scraping pada halaman utama getimg.ai dan mendapatkan judul-judul artikel pada website tersebut. Anda dapat menyesuaikan dengan elemen HTML lain yang ingin di-scrape dengan menggunakan selector yang sesuai.

Pastikan Anda telah menginstal library Axios dan Cheerio sebelum mencoba menjalankan kode di atas dengan menjalankan perintah npm install axios cheerio pada terminal

buatkan kode scraping website getimgai menggunakan cheerio

原文地址: https://www.cveoy.top/t/topic/ilA9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录