const axios = require('axios');
const cheerio = require('cheerio');
async function scrapeTopTikTokVideos() {
try {
const response = await axios.get('https://hypothetical-tiktok-website.com/top-videos');
const $ = cheerio.load(response.data);
const topVideos = [10];
// Replace the selectors below with the appropriate ones for the hypothetical website's video elements
$('div.video-item').each((index, element) => {
const title = $(element).find('h2.title').text().trim();
const url = $(element).find('a.video-link').attr('href');
const timestamp = $(element).find('span.timestamp').text().trim();
topVideos.push({
title,
url,
timestamp,
});
// Limit to the top 10 videos
if (topVideos.length === 10) {
return false; // Exit the .each() loop
}
});
return topVideos;
} catch (error) {
console.error('Error occurred during scraping:', error.message);
return [];
}
}
// Call the function to scrape the top 10 TikTok videos
scrapeTopTikTokVideos()
.then((topVideos) => {
console.log('Top 10 TikTok Videos of the Day:');
topVideos.forEach((video, index) => {
console.log(`${index + 1}. ${video.title}`);
console.log(` Posted At: ${video.timestamp}`);
console.log(` URL: ${video.url}`);
console.log('-------------------------');
});
})
.catch((error) => {
console.error('Error occurred:', error.message);
});